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

조회 수: 31 (최근 30일)
Joakim Magnusson
Joakim Magnusson 2015년 6월 24일
댓글: Stephen23 2015년 6월 25일
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
Joakim Magnusson
Joakim Magnusson 2015년 6월 24일
편집: Joakim Magnusson 2015년 6월 24일
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?

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2015년 6월 24일
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
Joakim Magnusson
Joakim Magnusson 2015년 6월 24일
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!
Stephen23
Stephen23 2015년 6월 24일
편집: Stephen23 2015년 6월 24일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Joakim Magnusson
Joakim Magnusson 2015년 6월 24일
편집: Joakim Magnusson 2015년 6월 24일
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)

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by