Filter löschen
Filter löschen

setappdata of multiple variables loaded from *.mat

2 Ansichten (letzte 30 Tage)
Jan w
Jan w am 2 Jan. 2017
Kommentiert: Jan w am 2 Jan. 2017
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 Kommentar
Niels
Niels am 2 Jan. 2017
if u are interested in creating gui's
here is a video in which Doug Hull is also talking about setappdata

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 2 Jan. 2017
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

Weitere Antworten (2)

Niels
Niels am 2 Jan. 2017
Bearbeitet: Niels am 2 Jan. 2017
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 am 2 Jan. 2017
You can store the variables in struct:
Data = load(FileName);
Then storing the variables with one setappdata together with their names is trivial.

Kategorien

Mehr zu Migrate GUIDE Apps finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by