Help with GUI callback - existence condition

1 visualización (últimos 30 días)
aurc89
aurc89 el 18 de Ag. de 2014
Comentada: Ben11 el 18 de Ag. de 2014
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

Adam
Adam el 18 de Ag. de 2014
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

Más respuestas (1)

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

Iniciar sesión para comentar.

Categorías

Más información sobre Interactive Control and Callbacks en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by