setappdata of multiple variables loaded from *.mat

3 ビュー (過去 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
  1 件のコメント
Jan w
Jan w 2017 年 1 月 2 日
Thanks 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 Center および File ExchangeMigrate GUIDE Apps についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by