passing variables from gui to another m file

hi
i have a gui interface whereby there is a drop down list to choose which data set you want. i have another my file that is dependent on the users choice of the dataset. how do i link the data from gui to another my file.
should i create a function
mydata=getdatamatirx(handles)
mydata = handles.data

 Accepted Answer

FAQ This should help.

5 Comments

i read through that but i dont know if it passes the variables through another m file
i have the following code
function Tests_Callback(hObject, eventdata, handles) % hObject handle to Tests (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
run test2 % another m file
function [myData,myHistory] = getPositions() myData = get(handles.AM,'Value') myHistory = myData + 1
THIS IS THE TEST2 SCRIPT [a,b] = getPositions()
BUT I GET THE ERROR Undefined function or variable 'getPositions'.
You would want to have getPositions take one input (handles) and then reference from inside. The fact that getPositions is showing up as not being findable means it's probably not on your MATLAB path.
use >>pathtool
to add the folder containg getPositions to your path.
if i reference the function from inside the gui m file, then how do i export the matrix variables to another m file?
what i am trying to do is:
from the gui the proper matrix data is extracted and then have to link that data to another m file. i am having trouble extracting the matrix to the other m file.
is there away to do this without having to define the matrices as global variables?
Yes, did you read the FAQ? use setappdata to store it and getappdata to retrieve it.
■Storing data in the application workspace using the SETAPPDATA and GETAPPDATA functions.
% Do this to save variables to your figure's workspace.
% handles.GUIHandle is the "Tag" property of your main GUI figure.
% Double-click figure to bring up the "Property Inspector" in GUIDE.
setappdata(handles.GUIHandle, 'yourVariable', yourVariable)
% Do this to retrieve variables from your figure's workspace.
yourVariable = getappdata(handles.GUIHandle , 'yourVariable')
% Do this to remove what you saved from your figure's workspace.
rmappdata(handles.GUIHandle, 'yourVariable')
You could also set to the root directory, i.e. 0., that way stuff is stored even after you close the GUI.
%Set
setappdata(0,'myStuff',stuff);
%Extract
mystuff = getappdata(0,'mystuff')

Sign in to comment.

More Answers (0)

Categories

Find more on Environment and Settings in Help Center and File Exchange

Asked:

on 24 Feb 2012

Edited:

Ben
on 24 Oct 2013

Community Treasure Hunt

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

Start Hunting!