while trying to use assignin to define value to a variable, for some reason, the variable is not created successfully although no error message appears
Show older comments
I have attached the code below. That command is at line 316. I just want to know why the variable cannot be created. Many thx.
4 Comments
@Yanda Jiang: rather than making variables magically jump from one worjkspace to another, you should simply pass the values as input/output arguments from your function. Passing values as input/output arguments is efficient and easy to debug, unlike what you are trying to do. Passing values as input/output arguments is the recommended best practice in the MATLAB documentation:
What you are trying to do, magically making variables appear in other workspaces, will always be slow, complex, and liable to bugs (like the bugs you are getting now):
Best solution (as the MATLAB documentation clearly states, and all experienced MATLAB users reccomend) is to pass values as output arguments.
Yanda Jiang
on 14 Aug 2018
Your code works for me, once I adjusted the strings to cell arrays (I use an older MATLAB version): those variables magically appeared in the base workspace, and I stopped the code.
"is there any way to fix this bug?"
Yes: avoid assignin and accessing variables dynamically by name.
" Because it is used in nested function?"
Nested functions do not allow dynamic changes to their workspaces, but in this case you assignin to the base workspace, so this should not be a problem. But if you are using nested functions (a good idea), then why do you need assignin at all?
Your basic concept should be improved: do NOT make variables magically appear in another workspace, unless you want to waste more of your time fixing pointless bugs like this one. You should use nested functions and simply use those names to define a structure with those fields. Get rid of those numbered variables, e.g. Tab1, etc, and use indexing with a cell array, then you can trivially loop over all groups or use indexing to select which one you are working with.
If you are writing a GUI, do NOT try to magically play with variables in the base workspace: pass all variables as input and output arguments. You might find waitfor useful.
Yanda Jiang
on 14 Aug 2018
Accepted Answer
More Answers (1)
Yanda Jiang
on 14 Aug 2018
0 votes
4 Comments
Walter Roberson
on 14 Aug 2018
I tried in R2018a.
If you put in the evalin('base','whos') after the loop that does the assignin(), then what output do you get?
Yanda Jiang
on 14 Aug 2018
Edited: Yanda Jiang
on 14 Aug 2018
Walter Roberson
on 14 Aug 2018
eval() would not have been looking in the right workspace. You would certainly have needed evalin() unless you were executing the code from the command line or from a script outside of the context of any function.
Yanda Jiang
on 14 Aug 2018
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!