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!