How would I implement the following without eval in?
2 次查看(过去 30 天)
显示 更早的评论
This is in a for loop with signalist being a list of variabeles in the workspace I wish to evaluate.
signalvalue = evalin('base', [signallist{i}]);
4 个评论
Stephen23
2019-12-6
编辑:Stephen23
2019-12-6
"How do I assign values from worspace values to be evaluated in a for loop?"
Experienced MATLAB users would simply pass the data as an output argument. Note that the question as you posed it is not answerable, as you did not explain anything about the context of that code: depending on where that code is used, there can be very different, but neat and efficient methods to pass it into the base workspace.
You can find methods to pass data betwen callback functions here:
"I want to assign "signalvalue" the contents of each variable one at a time in a for loop."
回答(1 个)
Image Analyst
2019-12-6
You might go over the several methods found in the FAQ: https://matlab.fandom.com/wiki/FAQ#How_can_I_share_data_between_callback_functions_in_my_GUI.28s.29.3F
Despite the name, most of the methods apply to functions in general, not just graphics callback functions.
2 个评论
Image Analyst
2019-12-7
If they've already been assigned then presumably you know their names and you can use those names in your code.
Disaster scenario follows:
If your variables are assigned dynamically and you have no idea what they're named (like some arbitrary, random program was run and loaded some variables into the base workspace and you have no knowledge of what they might be called because you don't know what program was run - a bad idea) then you'll have to get the names of the variables and get them into your program. But once you have them in your program's workspace, how will you USE them because the code after that point won't know the variables' names. (It's possible but we don't want ot encourage such coding). For example, let's say program1 puts var1 into the base workspace, and program2 puts var2 into the base workspace. Now let's say you have no idea which program had been run and you come along and you run a "program3.m" and for some reason it might need to get var1 and use it, or get var2 and use it. Let's say you wanted to assign it to var3. Well, if you don't know the name of the variable to retrieve from the base workspace (var1 or var2), would you do
var3 = var1;
or
var3 = var2;
Even if you knew it was either var1 or var2, you should know name var1 or var2. It wouldn't be a variable with some totally unknown name. If you had no idea and went to look in the base workspace and found, say 20 variables with various names in there, how on earth would you know which to get and stuff into var3?
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!