Hi,
If I write in command window "load a", then in workspace will appears variable "a". If I write "load a" in GUI code, so in workspace will apears nothing but variable "a" is available for GUI code. Why? Why is it not appears in workspace and why is available for GUI?
Thank you

댓글 수: 1

Daniel Shub
Daniel Shub 2012년 4월 11일
Loading a mat file, or a set of mat files, from a gui or function and poofing variables into the base workspace might be a case where I would be willing to use evalin, I doubt it, but maybe.

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

 채택된 답변

Jan
Jan 2012년 4월 11일

1 개 추천

Please read the Getting Started chapters of the documentation. There you find the explanation, that each function has its own workspace - a kind of container for the local variables - to avoid interferences. The command load imports the contents of the file in the current workspace, such that it appears in the base workspace, when it runs in the command windows. But when load is called inside a function, the imported variables are available only there. And this is important and safe.
To reduce confusions and unwanted side-effects it is recommended to store the output of load in a variable:
Data = load(FileName);
Then using Data.a shows directly, where the value is comming from. But in:
a = 3.1415;
load(FileName); % NOT CLEAN!
disp(a);
Now it is impossible to know the source of the value of a when reading the source code. This makes the debugging much harder.

댓글 수: 6

john
john 2012년 4월 11일
what is better to save and for load variable, save and load, or setappdat and getappdat, or uiputfile and uigetfile....I like popup window from uiputfile and uigetfile
Jan
Jan 2012년 4월 11일
SETAPPDATA store variables in an open figure, but not on the disk. UIPUTFILE gets a file name for saving, but does not write anything. Therefore I think, you yre looking for a combination of UIPUTFILE and SAVE.
john
john 2012년 4월 11일
for witch kind of combination are you thinking? Could you help me?
Jan
Jan 2012년 4월 11일
This saves the variable called "Data" to the file chosen by the user:
[FileName, PathName] = uiputfile('*.mat', 'Choose a file for saving:');
if ~ischar(FileName), return; end
save(fullfile(PathName, FileName), 'Data');
john
john 2012년 4월 11일
great code for me :), how do you know this?. I didn't find "save(fullfile(PathName, FileName), 'Data');" in matlab help.
.
.
.
can you help me with loading of variable ( this means with uigetfile and with load) ? please
john
john 2012년 4월 11일
I made loading for variable...so big thank you again

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Workspace Variables and MAT Files에 대해 자세히 알아보기

태그

질문:

2012년 4월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by