How to access to variables saved in Matlab folder from the GUI

I want to load my dataset or access any other variables that are saved to my matlab folder or are in my workspace currently from the matlab GUI I created. But when I call my functions on callback function of a button, I can't do it. Could you give me a simple example how to do it?

2 Comments

"any other variables that are saved to my matlab folder" &nbsp do you mean saved in a mat-file in the current folder?
See evalin
Yes exactly. I mean mat files

Sign in to comment.

 Accepted Answer

Add lines similar to the following, which are copied from the doc on load
filename = 'durer.mat';
myVars = {'X','caption'};
S = load(filename,myVars{:})
to the callback function.
&nbsp
" or are in my workspace" &nbsp see evalin, Execute MATLAB expression in specified workspace and add
my_variable = evalin( 'base', 'my_variable' )
to the callback function.

8 Comments

I want to select which database to work on on a popup menu so I tried following in my callback function according to your code:
contents = get(handles.popup_selectds,'String');
popupmenuvalue = contents{get(handles.popup_selectds,'Value')};
if popupmenuvalue== 'Dataset-1'
filename = 'dataset.mat';
S = load(filename)
elseif popupmenu4value== 'Dataset-2'
filename = 'dataset2.mat';
S = load(filename)
end
did not include
myVars = {'X','caption'};
section since I want all the data. But it did not load my datasets
"But it did not load my datasets" &nbsp Try to be more specific. Is S empty or what?
I happen to have a file named dataset.mat in my cssm-folder. It contains an instance of the dataset class of the Statistic Toolbox
>> S = load( 'dataset.mat' )
S =
cereal: [77x7 dataset]
>> whos -file dataset.mat
Name Size Bytes Class Attributes
cereal - 19106 dataset
well S seems
S =
dataset: [5000x21 table]
But I cannot see anything in my workspace and I don't know how to access a column in my dataset or how to call my .m files which has code uses this dataset.
Sorry about it when I could not see anything in the workspace I assumed It could not load anything properly. Because after I try to access to S it gives error.
S =
dataset: [5000x21 table]
>> S
Undefined function or variable 'S'.
  • There should be a struct named S in the workspace of the callback function.
  • Each variable of the mat-file should appear as a field of S
I guess you have a struct, S, with a field named dataset in the workspace of the callback function and want a copy of it in the base workspace.
Use assignin to make a copy in the base workspace
assignin( 'base', 'my_dataset', S.dataset )
Thank you now I can see my loaded dataset in my workspace but still I can't do anything on it.
contents = get(handles.popup_selectds,'String');
popupmenu4value = contents{get(handles.popup_selectds,'Value')};
if popupmenu4value== 'Dataset-1'
filename = 'dataset.mat';
S = load(filename)
assignin( 'base', 'dataset1', S.dataset )
dosomething1
elseif popupmenu4value== 'Dataset-2'
filename = 'dataset2.mat';
S = load(filename)
assignin( 'base', 'dataset2', S.dataset )
dosomething2
end
In the do something I basically try to access some data but I can't:
churn=dataset2.churn;
Undefined variable "dataset2" or function "dataset2.churn".
Error in yap (line 16) churn=dataset2.churn;
Error in ChurnP>popup_selectds_Callback (line 111) dosomething2
Error in gui_mainfcn (line 95) feval(varargin{:});
Error in ChurnP (line 42) gui_mainfcn(gui_State, varargin{:});
Error in @(hObject,eventdata)ChurnP('popup_selectds_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
Making the variables global and adding assigning solved my problem thank you very much for your help :)
"I can't do anything on it"
  • I assume that assignin actually assigned a variable in the base workspace
  • load assigns a value to the variable S.&nbsp dataset2 is not a variable, but a field of the struct S. Try churn = S.dataset2.churn;.
Comments

Sign in to comment.

More Answers (0)

Categories

Asked:

Ege
on 5 Jan 2015

Edited:

on 6 Jan 2015

Community Treasure Hunt

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

Start Hunting!