Select variable automatically in matlab

  • MATLAB
  • Thread starter mathia
  • Start date
  • #1
15
0
Hi,
I want to select my variable automatically in matlab so I used the following commands: (may variable for example are: Mpoint1,Mpoint5,Mpoint14,...)

for i=1:50
if exist (eval(['Mpoint' num2str(i)]));
statements
else
continue
end
end
but it gave me the following error:

Error using exist
The first input to exist must be a string.

Anybody know how can I do that?
Best regards,
Mathia
 

Answers and Replies

  • #2
1,065
54
baby steps...baby steps...


I don't know matlab, but I would suggest to first focus on getting the eval call to work, in the first place...then, you can worry about the loop, etc.

So, just try eval by itself until it is working...does it need a comma between the two arguments? I know you mean to concatenate, but it still may need to be separated by a comma in the list and EVAL will glue them togehter...that what EVAL does, I think.

Also, I read that EVAL takes any valid matlab statement...is what you provide to EVAL a valid statment? It is not really much of a command or assignment...will it work? maybe you have to assigne it to something and it will still return the value...

...play around, don't be shy...you are not going to break anything.
 
  • #3
15
0
I tested eval. It works correctly, but i have that error wen I use exist eval...!
 
  • #4
1,065
54
You may be overseeing the context within which the result is being used. 'exist' expects a string, not a command output, which is what eval returns.

You may need something like "strcat" instead...this is purely a string concatenation function...this may be all you need, not really eval.
 

Related Threads on Select variable automatically in matlab

Replies
1
Views
2K
Replies
1
Views
994
Replies
1
Views
2K
Replies
5
Views
595
Replies
5
Views
127K
Replies
2
Views
6K
  • Last Post
Replies
4
Views
3K
Replies
2
Views
2K
Replies
1
Views
25K
Replies
1
Views
4K
Top