Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

To resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

Exchange Data between functions

Asked by Hello kity on 2 Jan 2013

Hi,

how can I exchange data/filename between functions ?

see code:

function OpenMenuItem_Callback(hObject, eventdata, handles)
% hObject    handle to OpenMenuItem (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
 [filename, pathname] = uigetfile({'*.xls'},'File Selector');

I have an uiget file in the Openmenu item, selecting a xls file. That file should be used after using a (push)button.

function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

so the data in the filename should be here. currently i have another uigetfile in this function but then i need to select the file each time i press it... which i dont want, i want to select it once ..

popup_sel_index = get(handles.popupmenu1, 'Value');
switch popup_sel_index
     case 1
        Calculation(filename, handles) %other m-file
  end

I have read the help and other website but still dont get how exactly i can do this...

 thank you in advnace

0 Comments

Hello kity

Products

No products are associated with this question.

1 Answer

Answer by TAB on 2 Jan 2013
Edited by TAB on 2 Jan 2013
Accepted answer

Use gui handle object to to store the data. This handle will be common in all callback functions

function OpenMenuItem_Callback(hObject, eventdata, handles)
% hObject    handle to OpenMenuItem (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
 [filename, pathname] = uigetfile({'*.xls'},'File Selector');
 handles = guidata(hObject);         % Get handle in a variable
 handles.selectedfname = filename;   % Add your data in variable
 guidata(hObject, handles);          % Update the GUI handle with new value 

In other callback

function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles = guidata(hObject);          % Get handle in a variable
filename = handles.selectedfname;    % Read your data

1 Comment

Hello kity on 2 Jan 2013

This worked. Really greateful.

Somehow I find this part of matlab hard to understand :(

TAB

Contact us