Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: Opening struct in function
Replies: 3   Last Post: Feb 26, 2010 10:12 AM

 Messages: [ Previous | Next ]
 Ben Posts: 6 From: Netherlands Registered: 2/18/10
Re: Opening struct in function
Posted: Feb 26, 2010 7:29 AM

Thanks, I know, have to learn a lot.

I'll try my best.

The goal of my function is to overwrite a matrix in example.mat.
The problem is how can I make from a string a variable. Cause in the input of my function I use the string 'data.direction.A'.
I recently discovered eval(...), which makes a variable from a string .

But here comes the problem. I can get the data from the struct. But I can't write it back using eval(data.direction.A), because
eval(data.direction.A) = C; does not work at rule 17 in the function below.

Also D=eval(data.direction.A);
D=C; doesn't work.

How can I fix this problem? Thanks in advance.

function []=FgrowingDataSet(structname,pos,datainput)

% input:
% structname: name.mat
% pos: Matrix needs to be filled with enough zeros. example: data.direction.A, which is located in name.mat.
% datainput: selected array of data
% Data will be added if: data is not the same and if data is not zero.

% Made by Ben

2. C = eval(pos); %makes from string variable
3. i = 1;
4. if C(1,1)==0;
5. C(1,1:length(datainput)) = datainput;
6. else
7. while C(i,1) ~= 0;
8. if C(i,1:length(datainput)) == datainput;
9. break
10. else
11. i=i+1;
12. end
13. end
14. C(i,1:length(datainput)) = datainput;
15. end
16.
17. data.direction.A = C;
18. save(structname, 'data', 'structname');
19. end

Date Subject Author
2/23/10 Ben
2/23/10 Oleg Komarov
2/26/10 Ben
2/26/10 Steven Lord