I have a variable in the MATLAB workspace and I want to pass this variable to a function in my GUI.
How do I achieve this task?
I have a variable in the MATLAB workspace and I want to pass this variable to a function in my GUI.
How do I achieve this task?
You can use SETAPPDATA (in the main workpsace) and GETAPPDATA (in GUI) functions.
If you variable is someMatrix
setappdata(0,'someMatrix',someMatrix) % in the main workspace
someMatrix = getappdata(0,'someMatrix') % in GUI
You can use the function EVALIN in your GUI to get the value of a variable from the base workspace. The following example extracts the value of the variable A
in the base workspace and places that value in the local variable B
:
B = evalin('base','A');
You could, for example, have an editable text box in your GUI that allows the user to enter the name of a variable to import from the base workspace. One of your GUI functions could then read the string from the editable text box and attempt to fetch that variable from the base workspace to use in some computation:
varName = get(hEditText,'String'); %# Get the string value from the uicontrol
%# object with handle hEditText
try %# Make an attempt to...
varValue = evalin('base',varName); %# get the value from the base workspace
catch exception %# Catch the exception if the above fails
error(['Variable ''' varName ... %# Throw an error
''' doesn''t exist in workspace.']);
end