开发者

Display of MATLAB workspace variable into GUI function

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开发者_Python百科 this task?


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


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜