0

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?

Rhei
  • 127
  • 11
  • 1
    Callback functions have a specific definition, you cannot just add input or output arguments as you please. `load_data_Callback` must save the data in the UI, and `pushbutton2_Callback` must fetch the data from where it was saved. You can use, for example, https://www.mathworks.com/help/matlab/ref/guidata.html or https://www.mathworks.com/help/matlab/ref/getappdata.html – Cris Luengo Oct 29 '18 at 20:52

1 Answers1

2

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');
%...%
Brice
  • 1,560
  • 5
  • 10
  • `userdata` is deprecated, now it is best to use `setappdata` and `getappdata`. For storing values other than graphic handles, it is even recommended over the use of `guidata`, which is best kept to only store your graphic handle structure. The principle remain the same so your answer is still valid, but you could do with rewritting a more current version. – Hoki Oct 30 '18 at 16:58
  • As of R2018b, all three methods are documented by Mathworks as a way to [share data among callbacks](https://www.mathworks.com/help/matlab/creating_guis/share-data-among-callbacks.html). I have added an example with setappdata/getappdata for completeness. – Brice Oct 30 '18 at 17:29