Help with GUI callback - existence condition
Mostrar comentarios más antiguos
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.
Respuesta aceptada
Más respuestas (1)
Ben11
el 18 de Ag. de 2014
You can check for the non-existence of a variable using this line:
if ~exist('a','var')
% Code
else
% code
end
4 comentarios
aurc89
el 18 de Ag. de 2014
Ben11
el 18 de Ag. de 2014
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
el 18 de Ag. de 2014
Ben11
el 18 de Ag. de 2014
you're welcome! If my answer helped you can give it a vote up :) Glad to help!
Categorías
Más información sobre Structures en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!