Help with GUI callback - existence condition
조회 수: 2 (최근 30일)
이전 댓글 표시
Hello! I'm working on a GUI in Matlab, and I have a simple question. I have two 'Push button' commands, let's say 'Button1' and 'Button2', and two 'Edit text' commands. The two 'Edit text' commands are tagged as 'a' and 'b'. Let's suppose I write two numbers in these two Edit text commands, and by pressing 'Button 1' I save them in a handle structure inside the callback function of 'Button1' (e.g. a = str2num (get(handles.a,'String'));b = str2num (get(handles.b,'String')); handles.num1=a; handles.num2 = b). Then, by pressing 'Button 2', inside its callback function I recall these two numbers (num1=handles.num1; num2=handles.num2 - e.g. num1 = 98 and num2 = 21) and do an operation between them.
Now, let's suppose I want to press directly 'Button 2' to perform the same operation between num1 and num2 but without writing them in the edit text commands: of course the callback function of 'Button 2' will not recognize any 'num1' and 'num2', because I don't save them in any handle structure before. What I want to do is to write this condition in the 'Button 2' callback: " If 'num1' and 'num2' don't exist - i.e. the condition satisfied by pressing directly 'Button 2' - then set num1 = 100 and num2 = 20 "; then the rest of the code will be the same. How can I translate this into a code inside the 'Button 2' callback function? Thank you very much.
댓글 수: 0
채택된 답변
Adam
2014년 8월 18일
isfield( handles, 'num1' )
will tell you if num1 exists as a field in your handles structure.
It isn't necessarily the way I would suggest going about what you are trying to do, but if I understand your question correctly it will achieve what you want, though only on the first press of button 2 before button 1. If button 1 is still creating these fields then they will exist for the lifetime of your GUI on handles after their first creation unless you explicitly remove them
추가 답변 (1개)
Ben11
2014년 8월 18일
You can check for the non-existence of a variable using this line:
if ~exist('a','var')
% Code
else
% code
end
댓글 수: 4
참고 항목
카테고리
Help Center 및 File Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!