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

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.
Hi Stephen, Thx for your prompt reply. Actually, I also want to use input/output, but I only have the name of variables here, which are stored in Tab cell array, but not variables themselves. I cannot transfer values to variables as normal. This is the reason why I need to use assignin function. So right now, is there any way to fix this bug? Why does this hsppen? Because it is used in nested function? Many thx in advance.
Sincerely, Yanda
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.
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

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

Hi Walter, Thx for your reply, but my question is not that. I can deal with that later. So right now what made me confused is at line 314, that assignin function. It doesn't input value into workspace correctly and respond no error message. Right now, since I only have variable names stored in "Tab" cell array, assignin is a easier way to implement it. So could you please tell me the reason for this issue and how to fix it if possible? Many thx in advance.
Sincerely, Yanda
K>> evalin('base','whos')
Name Size Bytes Class Attributes
CapControl 0x19 0 cell
Capacitor 0x20 0 cell
Line 0x32 0 cell
Linecode 0x26 0 cell
Load 0x43 0 cell
Reactor 0x28 0 cell
RegControl 0x32 0 cell
Transformer 0x44 0 cell
Vsource 0x30 0 cell
XfmrCode 0x34 0 cell
It is working for me. Which MATLAB version are you using? I tested in R2016b as well .
2018a. I find they are in workspace after program is ended, but not while it is still running... is that same in yours,
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)

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

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?
I tried to use those variables and got response they were not existed, so I was totally confused...I though I used eval, but not evalin.
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.
I see. I will try it again. Thx so much for sharing this information.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!