A program to check the existence of a variable in the Workspace
1 回表示 (過去 30 日間)
古いコメントを表示
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 件のコメント
採用された回答
Walter Roberson
2015 年 9 月 12 日
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''))
0 件のコメント
その他の回答 (1 件)
Image Analyst
2015 年 8 月 31 日
編集済み: Image Analyst
2015 年 8 月 31 日
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 件のコメント
Image Analyst
2015 年 9 月 12 日
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.
参考
カテゴリ
Help Center および File Exchange で Environment and Settings についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!