Select variable automatically in matlab

In summary, the conversation involves a person seeking help with using the "eval" command in MATLAB to automatically select a variable. They are struggling with getting the command to work and are seeking advice on how to use it correctly. Suggestions were made to focus on getting the eval call to work first and to try using "strcat" instead of "eval" since "exist" expects a string as its first input. The person was encouraged to experiment and not be afraid to make mistakes.
  • #1
mathia
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?

Mathia
 
Physics news on Phys.org
  • #2
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.
 
  • #3
I tested eval. It works correctly, but i have that error wen I use exist eval...!
 
  • #4
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.
 
  • #5
s,

There are a few things that could be causing this error. First, make sure that all of your variables are actually strings. You can use the class() function to check the data type of your variables. If any of them are not strings, you can use the num2str() function to convert them.

Another possible issue is that the eval() function is not necessary in this case. You can use the exist() function directly on the variable name without using eval().

Finally, it's always a good idea to use error-checking in your code. You can use the isvarname() function to check if a given string is a valid variable name before using it in your code.

I hope this helps and good luck with your project!
 

Related to Select variable automatically in matlab

1. How does the "Select variable automatically" function work in MATLAB?

The "Select variable automatically" function in MATLAB is used to automatically select and assign a variable from a given set of variables based on certain criteria. It uses a default algorithm to determine the most appropriate variable based on data type, size, and other factors.

2. Can I customize the criteria used by the "Select variable automatically" function in MATLAB?

Yes, you can customize the criteria used by the "Select variable automatically" function in MATLAB. You can specify the order in which the variables should be evaluated, as well as define your own criteria for selecting the variable.

3. How can I use the "Select variable automatically" function to improve my code?

The "Select variable automatically" function can be useful in simplifying and improving your code by automatically selecting the appropriate variable for a given task. This can save time and effort in manually selecting variables and can also help avoid errors.

4. Is the "Select variable automatically" function available in all versions of MATLAB?

Yes, the "Select variable automatically" function is available in all versions of MATLAB. However, the functionality and syntax may vary slightly between versions.

5. Can I use the "Select variable automatically" function with user-defined variables?

Yes, the "Select variable automatically" function can be used with user-defined variables. However, it is important to ensure that the variable names are unique and do not conflict with any built-in MATLAB functions or variables.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
598
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
3K
Back
Top