Rhei
Rhei

Reputation: 127

Matlab GUI programming: use data imported with uiimport

I am programming a simple GUI which has to do 2 tasks:

1- import a set of data for a txt file

2- make some computation with the previously imported data

Both tasks are performed pressing a button, one button for each task.

For the "Load data" button (task 1) I used the uiimport command in the Callback of that button (as explained here Matlab Calling 'Import Data' with GUI Button):

S = uiimport('-file');

The data are loaded as a "191384x3 double". I also modified the function as follow, in order to have S available for the 2nd button:

function S = load_data_Callback(hObject, eventdata, handles)

Then I press the second button to perform the 2nd task. In the Callback of the 2nd button I wrote

function pushbutton2_Callback(hObject, eventdata, handles, S)

loaded_data = S; % to access the data in the non structured array
% Then I want to have 3 separate vectors out of the structure
v1 = loaded_data(:,1);       
v2 = loaded_data(:,2);     
v3 = loaded_data(:,3);

When I press the 2nd button I get an error message:

Not enough input arguments.
loaded_data = S;
Error in gui_mainfcn (line 95)
    feval(varargin{:});

What am I missing?

Upvotes: 0

Views: 411

Answers (1)

Brice
Brice

Reputation: 1580

You need to store the output from the first function somewhere, where the second function will be able to access it. This is typically done by storing it either in some ui object UserData property, or to store in some special data storage structure that is attached attached to figure objects, and is accessed with either the guidata function, or the getappdata and setappdata functions.

dat=guidata(hObj); gets the guidata from hObj parent figure

guidata(hObj,dat); sets the guidata of the parent figure of hObj to dat

Working with guide, the guidata structure is populated by default with a structure containing all the ui objects, named after their tag. Extra fields may be added as needed. The handle argument from callbacks contains guidata(hObj).

Three possible implementations:

1. With guidata

Store the loaded data with guidata

function load_data_Callback(hObject, eventdata, handles)
% Load the file and save the result in variable S%
% Then: %
handles.S=S;
guidata(hObject,S);

It will be automatically loaded back in the handles input variable

function pushbutton2_Callback(hObject, eventdata, handles)
assert(isfield(handles,S),'Load some data first!');
loaded_data=handles.S;
%...%

2. With Userdata property (up to R2014a)

Store the loaded data into some uiobject UserData, e.g. pushbutton2

function load_data_Callback(hObject, eventdata, handles)
% Load the file and save the result in variable S%
% Then: %
set(handles.pushbutton2,'UserData',S,'Enable','on'); %You could disable pushbutton2 by default, until some data has been loaded

Recover the data from the UserData property

function pushbutton2_Callback(hObject, eventdata, handles)

loaded_data=get(hObject,'UserData');
assert(~isempty(loaded_data),'Load some data first!');
%...%

3. With appdata functions

Store the loaded data with setappdata

function load_data_Callback(hObject, eventdata, handles)
% Load the file and save the result in variable S%
% Then: %
setappdata(hObject,'loaded_data',S);

Recover with getappdata:

function pushbutton2_Callback(hObject, eventdata, handles)
assert(isappdata(hObject,'loaded_data'),'Load some data first!');
loaded_data=getappdata(hObject,'loaded_data');
%...%

Upvotes: 2

Related Questions