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

1 view (last 30 days)
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
  2 Comments
Paul Nanfah
Paul Nanfah on 12 Sep 2015
I did know there can be many workspaces, I guess the one i want to check variable in is the main Worspace, the one which is currently running

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 12 Sep 2015
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''))

More Answers (1)

Image Analyst
Image Analyst on 31 Aug 2015
Edited: Image Analyst on 31 Aug 2015
Get the 'String' property instead of the data property:
str = get(handles.input, 'String');
And then you need to pass 'var' in to exist() as the second argument. And str will already be a string. You don't need to even use textscan() to create another string. Plus, t will exist if you did use textscan() so there is no need to use exist() to check for it since it will always exist. So basically the whole code boils down to this:
t = get(handles.input, 'String');
set(handles.output, 'String','yes'); % No brackets needed.
  4 Comments
Paul Nanfah
Paul Nanfah on 12 Sep 2015
hi, I associate text box with the tag 'output' , the edit box with 'input' and the button with 'check', i guess the graphic i am getting with handles.output is the right one, namely the text box
Image Analyst
Image Analyst on 12 Sep 2015
No - the error message is telling you that "output" is the name of your figure, not of an edit text box. Look, you tried this:
set(handles.output,'String',['yes']);
and it said this:
The name 'String' is not an accessible property for an instance of class 'figure'.
So it thinks output is the name of your figure, not an edit text box. Try changing the names of your edit text boxes. I don't think it should make any difference but try getting rid of the brackets around the 'yes' string.

Sign in to comment.

Categories

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

Community Treasure Hunt

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

Start Hunting!