필터 지우기
필터 지우기

setappdata of multiple variables loaded from *.mat

조회 수: 2 (최근 30일)
Jan w
Jan w 2017년 1월 2일
댓글: Jan w 2017년 1월 2일
Hello,
On my GUI I'm loading a *.mat file containing 15 variables that should be saved to appdata.
How can I
setappdata(0,'x',x);
for all variables at once with variable name and avoid to set 15 variables one at the time?
Do I have to necessarily load(*.mat) into workspace or is there a direct way to setappdata after uigetfile?
Regards
  댓글 수: 1
Niels
Niels 2017년 1월 2일
if u are interested in creating gui's
here is a video in which Doug Hull is also talking about setappdata

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

채택된 답변

Walter Roberson
Walter Roberson 2017년 1월 2일
data_struct = load('NameOfMatFile.mat');
fn = fieldnames(data_struct);
for K = 1 : length(fn)
this_field = fn{K};
setappdata(0, this_field, data_struct.(this_field));
end

추가 답변 (2개)

Niels
Niels 2017년 1월 2일
편집: Niels 2017년 1월 2일
Hi jan,
if you are working at a gui it would be better to save the appdata inside the figure, so that the variables are automatically deleted whenever you close the figure and you dont have to remove them manually.(usually there are no problems, but it depends on where u saved your Callback Functions -> just try it)
if u work with GUIDE (and you didnt change the tag of the figure) it is
setappdata(handles.figure1,'Name',Value)
if you do not work with gui, but used guihandles its still the tag of your figure.
setappdata(handles.TagOfYourFigure,'Name',Value)
You can save the 15 Variables in one cell array or structur and save the cell/ strucur
structur would be easier to handle :D
like
Data.VarName1=VarName1
Data.Varname2=VarName2
...
setappdata(handles.figure1,'DataSavings',Data)
get them back with
Data=setappdata(handles.figure1,'DataSavings')
concerning the issue to put the 15 Variables into 1 Variable, there is a way, but i think it might not work for u:
with
list=who
you get all variablNames from the Variables which are saved at your workspace and with
for i=1:numel(list)
Data{i}=evalin('base',list{i})
end
you save their Values (without Names) in Data. But it would be hard to get the right Variable back if you dont know in which order you declared the Variables. And if you declared more than these 15 Variables, you got a Problem.
but maybe u might think of some modifications that help u
Greeting Niels

Jan
Jan 2017년 1월 2일
You can store the variables in struct:
Data = load(FileName);
Then storing the variables with one setappdata together with their names is trivial.

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by