Help with GUI callback - existence condition

2 ビュー (過去 30 日間)
aurc89
aurc89 2014 年 8 月 18 日
コメント済み: Ben11 2014 年 8 月 18 日
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.

採用された回答

Adam
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 件のコメント
aurc89
aurc89 2014 年 8 月 18 日
Thanks!

サインインしてコメントする。

その他の回答 (1 件)

Ben11
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
aurc89 2014 年 8 月 18 日
Thank you so much :)
Ben11
Ben11 2014 年 8 月 18 日
you're welcome! If my answer helped you can give it a vote up :) Glad to help!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMigrate GUIDE Apps についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by