save(filename, var) cant find workspace variable even if it exist

From my script i am trying to use save to store my workspace stucture to a .mat file. But i get following error message:
>> TestGUI Error using save Variable 'NewTest' not found.
In my script i use save like this:
save('NewTest.mat','NewTest');
I can see that my structure exists in the workspace, any idea why save don't find my variable?

3 Comments

Can you please show us a screen shot of the workspace containing this variable?
Does the save call have access to that workspace?
Here is a screenshot.
I'm just using 'base' workspace, i put the structure in my workspace from my code so i should have access i think?

Sign in to comment.

 Accepted Answer

The error message and the quoted code code give two different variable names: 'NewTest' vs 'newTest'.
Capitalization is significant in MATLAB!
It is not possible for both of these to be correct, so which one is right?

7 Comments

I'm sorry, posted wrong error message. I have updated the question now.
So you can see that variable in your workspace, but which workspace? Every function has its own workspace, and unless these are nested functions they do not have access to variables which are inside other functions, or in the base workspace.
The workspace shown here seems to be the base workspace, which has nothing to do with the workspace of the function pushbutton_saveTest_Callback. There is no variable in its workspace called NewTest, so you get the error.
Note that there are variables listBoxItems and newTestName which do not appear in the workspace in the screen-shot: does this make you a little suspicious about which workspace this is? (although an error would remove then anyway..)
Lines 293 has newTest, line 302 has NewTest. Is this intentional?
The solution is to learn to use MATLAB's debugging tools properly. This lets you view any workspace, step through code and detect when specific events occur, and much more. Set a break point in that function (e.g. line 281), and then step through using F10 button. It will show you the function's workspace (not the base workspace), and you can see which variables actually exist there...
Although you might be tempted to access data in the base workspace from withing functions, I would advise against this. This is poor programing style, and not at all robust. Instead learn how to pass variables properly between functions and between workspaces. For example, using assignin and evalin are usually signs of bad variable handling.
I have never used anything other than the base workspace so i'm not sure how multiple workspaces work, but if it matters, all my code is in the same .m file and i think i only use the base workspace.
line 293 should be commented out.
I'm sorry if my code is confusing. I want to use:
save([newTest '.mat', newTest)
or something like that, i just changed it to string 'NewTest' to be sure that variable newTest contained the right string.
Isn't my functions workspace and the base workspace the same workspace?
Okay i think i understand what you mean now. But as you can see i assign newTestName to the base workspace.
So...the problem should be that save is trying to access 'NewTest' in my fynctions workspace? In that case, how do i make save access the base workspace?
"I have never used anything other than the base workspace" is not true, because every time you create a function it has its own workspace.
"Isn't my functions workspace and the base workspace the same workspace?" No. This is clearly explained in the links that I gave you.
"...how do i make save access the base workspace?" That depends on you. My advice (and what I would write myself) would be to pass variables correctly between workspaces, and not access the base workspace like this. However if you really do want to keep the variables in the base workspace and write some non-robust slow hack code, then use assignin and evalin.
Thanks for the help, i understand how the different workspaces work now i think, i didn't know that every function had it's own workspace!
My pleasure!
It is levels of abstraction: suddenly thinking about multiple workspaces also means more complicated planning... but also more power! Please ask if you have any questions about how to use workspaces. Start small, look at the docs, find some examples on FEX, try out how it works... Good luck!
EDIT: one of the key things to understanding about functions is that they encapsulate and abstract some operation. It should not be required to poke around other workspaces because the whole point of a function is that it is a black box: all that matter is the input and output, not what happens in between (i.e. how it does it). This is the point of having separate workspaces... they keep operations conceptually independent.

Sign in to comment.

More Answers (1)

I found a solution. Instead of assigning my struct to the base workspace, i just created a variable with my struct within my function, and then just used save like this, i guess the variable i created exists in the function workspace and that save is accessing the functions workspace, correct me if i'm wrong!
structData = getNewTest(handles,newTestName);
save('NewTest.mat','structData');
(getNewTest returns the struct i want to save)

Products

Tags

Community Treasure Hunt

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

Start Hunting!