Help with GUI callback - existence condition
이전 댓글 표시
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.
채택된 답변
추가 답변 (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
aurc89
2014년 8월 18일
Ben11
2014년 8월 18일
Nope you would have to use 2 calls to ~exist:
if ~exist('a','var') && ~exist('b','var')
Actually Adam's answer is more appropriate for the handles structure; but it's good to know this alternative :)
aurc89
2014년 8월 18일
Ben11
2014년 8월 18일
you're welcome! If my answer helped you can give it a vote up :) Glad to help!
카테고리
도움말 센터 및 File Exchange에서 Structures에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!