MATLAB: A program to check the existence of a variable in the Workspace

check variableexistguiworkspace

hello everyone,
I first build a GUI looking like this
the static textfield should return a YES if there is variable and a NO if not.
and the code:
function input_Callback(hObject, eventdata, handles)
% --- Executes on button press input check.
function check_Callback(hObject, eventdata, handles)
str = get(handles.input, 'data');
t = textscan(str, '%s');
if exist ('t')
set(handles.output, 'String',['yes']);
else
set(handles.output, 'String',['No']);
end
can someone explain to me what i am doing wrong ?
thanks

Best Answer

There is no "main" workspace. There is a "base" workspace which always exists but it is not "the one which is currently running".
Every function that is currently executing has a workspace, and there are also workspaces hanging around for some kinds of functions if their function handle has been taken and saved somewhere that still exists.
If you are thinking that a GUI that is "executing" has a "main workspace" then that would be incorrect. A GUI is a figure that has uicontrol and uimenu and related objects stored under it. When one of the controls is activated, the appropriate responding code is fetched from the control (the callback routine) and that routine is started and has a workspace as long as it is executing. And then when the callback routine exits, that workspace is destroyed. If you get back to the command line prompt then there are no active workspaces, just the base workspace. When a GUI is sitting waiting for input, it is not active, it is reactive and it has no workspace.
You can check the base workspace to see if a variable exists by using
evalin('base','exist(''VariableNameHere'',''var''))