Select variable automatically in matlab

  • Context: MATLAB 
  • Thread starter Thread starter mathia
  • Start date Start date
  • Tags Tags
    Matlab Variable
Click For Summary

Discussion Overview

The discussion revolves around selecting variables automatically in MATLAB using dynamic variable names. Participants are exploring the use of the `eval` and `exist` functions to achieve this, addressing issues related to syntax and functionality within a loop context.

Discussion Character

  • Technical explanation
  • Exploratory
  • Debate/contested

Main Points Raised

  • One participant describes an attempt to use a loop with `eval` and `exist` to check for the existence of dynamically named variables, encountering an error related to the input type for `exist`.
  • Another participant suggests focusing on getting the `eval` function to work correctly before addressing the loop, questioning whether the syntax is appropriate for `eval` and if it requires a comma for concatenation.
  • A later reply confirms that `eval` works correctly but highlights that the error arises when using `exist` with the output from `eval`.
  • Another participant proposes that `exist` requires a string input and suggests using `strcat` for string concatenation instead of relying on `eval`, indicating that this might resolve the issue.

Areas of Agreement / Disagreement

Participants have not reached a consensus on the best approach to resolve the issue, with multiple competing views on how to correctly use `eval` and `exist` in this context.

Contextual Notes

There are limitations regarding the assumptions about the variable names and the specific context in which the commands are being executed. The discussion does not resolve how to properly format the input for `exist` when using `eval`.

mathia
Messages
15
Reaction score
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?

Mathia
 
Physics news on Phys.org
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 statement? 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.
 
I tested eval. It works correctly, but i have that error wen I use exist eval...!
 
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.
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K