Borrar filtros
Borrar filtros

Creating a counter loop in MatLab, GUI?

9 visualizaciones (últimos 30 días)
Amed
Amed el 25 de En. de 2013
Editada: Irwin2020 el 22 de Nov. de 2018
Hello, i'm trying to create a loop counter that will add increments of 1 every time I click a pushbutton. I don't really have any code as of yet since i'm new to programming but i've figured how to detect the button being pushed, and display a number in the static text. It keeps displaying '1', how do I display 1,2,3,.....n for every push? Here is what I have so far. Thanks a bunch
a = 0;
if (handles.pushbutton5)
a = a + 1;
set(handles.text8,'string',a);
end;
  3 comentarios
Jan
Jan el 29 de En. de 2013
@Azzi: As Walter has explained before when I've asked the same question, the "if true, ..., end" appears, when you hit the "code" button when no text is selected.
Wouter
Wouter el 7 de Nov. de 2013
And what if I want the option to count backwards?
I've tried creating a second pushbutton that produces:
counter = get(hObject, 'UserData') - 1;
However, no effect.

Iniciar sesión para comentar.

Respuesta aceptada

Jan
Jan el 29 de En. de 2013
I would avoid mixing the string and the numerical representation, although both are equivalent in this case. So you can add in the opening function:
set(handles.pushbutton5, 'UserData', 0);
and in the callback of this pushbutton:
function pushbutton5_Callback(hObject, EventData, handles)
counter = get(hObject, 'UserData') + 1;
set(hObject, 'UserData', counter);
set(handles.text8, 'String', sprintf('%d', counter));
If the field "text8" is missing, see FAQ: incomplete handles struct
  4 comentarios
Jan
Jan el 29 de En. de 2013
It initialized the UserData property to 0. Otherwise the default is [].
Irwin2020
Irwin2020 el 22 de Nov. de 2018
Editada: Irwin2020 el 22 de Nov. de 2018
Jan , can you please help me create a counter -loop adding strings (Hex values)?

Iniciar sesión para comentar.

Más respuestas (3)

Azzi Abdelmalek
Azzi Abdelmalek el 25 de En. de 2013
set(handles.text8,'string',num2str(a));
  5 comentarios
Azzi Abdelmalek
Azzi Abdelmalek el 25 de En. de 2013
Look at the above code
Azzi Abdelmalek
Azzi Abdelmalek el 25 de En. de 2013
In opening function initialize
Set(handles.text8,'string','0')

Iniciar sesión para comentar.


Amed
Amed el 25 de En. de 2013
Editada: Amed el 25 de En. de 2013
Anybody else?
i initialized it as
set(handles.text8,'string','0')
a = str2double(get(handles.text8,'string'));
if (handles.pushbutton5)
a = a + 1;
set(handles.text8,'string',a);
end;
but get a constant '1' answer every click.. it will not add +1 to each click and display
  1 comentario
Azzi Abdelmalek
Azzi Abdelmalek el 29 de En. de 2013
Editada: Azzi Abdelmalek el 29 de En. de 2013
The initialization is not in the right place. You should do it in your opening function, not in your pushbutton function

Iniciar sesión para comentar.


Amed
Amed el 29 de En. de 2013
Anybody else have any ideas? Still not solved
  2 comentarios
Jan
Jan el 29 de En. de 2013
Editada: Jan el 29 de En. de 2013
Please post comments in the comment section and not as an answer. Thanks.
simran
simran el 28 de Abr. de 2013
put your initial value for counter in Property Inspector's User data property but not in pushbutton callback function. This will increment counter on every click of push button.

Iniciar sesión para comentar.

Categorías

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

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by