How can I convert a string which contains a variable name into a variable which can be called?

2 次查看(过去 30 天)
I am creating a function which scans files for a certain function and determines which variables (all are already initialized) are used as parameters for the function. Currently, I am able to derive a cell array with strings for each individual variable. The program takes this:
x = DummyFunction(a, b, c);
And returns this:
{'a'} {'b'} {'c'}
I am trying to convert these strings, which contain pre-established variables, into variables which can be called. Any suggestions?
Thanks in advance, Ak
  2 个评论
Jan
Jan 2018-4-26
This means, that you are trying to create a Matlab interpreter. Are you sure that this meta-programming is useful? Matlab is a perfect Matlab interpreter already. So what about using the debugger?
Stephen23
Stephen23 2018-4-26
"I am trying to convert these strings, which contain pre-established variables, into variables which can be called. Any suggestions?"
The best suggestion is Do NOT do this. Dynamically accessing variable names uis slow, complex, and buggy. Dynamically accessing variable names is how beginners force themselves into writing slow, complex code that they struggle to debug. Read this to know why:
There are easier ways to achieve what you want: fields of a structure, table variables, indexing, etc. All of these will be simpler, neater, and much more efficient.

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2018-4-26
You do not need to do that.
Put all of the variables into a structure, using dynamic field names. Then when you need to recall them by name,
Variables_for_this_call = cellfun(@(name) StructureOfVariables.(name), ListOfNames, 'uniform', 0);
After which you can
DummyFunction(Variables_for_this_call{:})
  2 个评论
Ak K
Ak K 2018-4-27
Walter, thank you for responding to my question.
In my final program, I will have a GUI where the user will be able to input many different parameters for a variety of calculations. The user will choose the exact calculation process. Based on the calculation type the user chooses, the program will execute it with different parameters (which are determined by the calculation type chosen) inputted in one common function. My goal is to make a second function which can plot the parameters used during the calculation. For this reason, I need to scan the calculation file for the specific parameters used when the calculation is run by the user. Since it is a GUI Structure, all of these parameters have already been initialized and in order to find their values, I just need to run code like this:
Parameter1 = handles.x
For this reason, I need a way of converting the string "handles.x" (or cell {'handles.x'}, whichever is easier) into a variable which can be called as in the operation above. Is this possible? If not, I would also appreciate a means of calculating the value of the variables contained in the strings directly without actually converting the string into a callable variable name.
Also, since I am fairly new to MATLAB, I am somewhat unsure as to what you wrote above. Could you please 'dumb-it-down' a little bit? Sorry for the inconvenience. Again, thank you for responding to my question.
Stephen23
Stephen23 2018-4-27
编辑:Stephen23 2018-4-27
" Could you please 'dumb-it-down' a little bit?Í"
Sure.
"...I need a way of converting the string "handles.x" ... into a variable..."
Don't do that. Just access handles.x directly, you can even use dynamic fieldnames:
fnm = 'x';
data = handles.(fnm);
That is our advice in a nutshell. Read the link I gave in my comment to know more about why your code concept will force you into writing slow, complex, buggy code.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by