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

5 views (last 30 days)
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
Stephen23
Stephen23 on 14 Aug 2018
Edited: Stephen23 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
Yanda Jiang on 14 Aug 2018
Thx so much for your help, Stephen. Yes, I have realized input/output is much more reliable. I will remember this lesson.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 14 Aug 2018
In Tab_group_SellectCallback you have
TabSellectCallback(0,0,group,1);
The 1 in the fourth parameter becomes the variable num inside TabSellectCallback. The code there has
for Tab_group_temp = 1:num_group
for TabNumber_temp=1:num(Tab_group_temp)
num_group is 6, so the outer loop is 1:6, and that value is used to index num on the second of those lines. With it being possible for Tab_group_temp to be 6, it follows that the variable num must have at least 6 values. But it does not have 6 values: it is a scalar with value hard-coded as 1.
Now, Tab_group_SelectCallback is called as
Tab_group_SellectCallback(0,0,1);
where 1 is the group. At the point of that call, there is an existing variable num that contains the information that is needed on the line
for TabNumber_temp=1:num(Tab_group_temp)
but those two num are not the same variables because num is not being passed into Tab_group_SellectCallback and Tab_group_SellectCallback is calling TabSellectCallback with hard-coded scalar 1.
The repair would be to arrange to pass num into Tab_group_SellectCallback, which should then pass it into TabSellectCallback instead of the hardcoded 1.
  4 Comments
Yanda Jiang
Yanda Jiang on 14 Aug 2018
2018a. I find they are in workspace after program is ended, but not while it is still running... is that same in yours,
Walter Roberson
Walter Roberson on 14 Aug 2018
I don't see that happening. I put a
evalin('base','whos')
in the file after the loop that does the assignin(), and everything shows up. It looks to me as if the assignin() are being done.

Sign in to comment.

More Answers (1)

Yanda Jiang
Yanda Jiang on 14 Aug 2018
Then I don’t know why... it doesn’t work for me, so I come up with another method to circumvent it. Maybe version difference is the reason. Thx though for your help and patience. I really appreciate that.
  4 Comments
Walter Roberson
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.

Sign in to comment.

Categories

Find more on Variables 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!