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

Azzi Abdelmalek
Azzi Abdelmalek 2013년 1월 25일
What "if true" is for?
Jan
Jan 2013년 1월 29일
@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 2013년 11월 7일
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.

댓글을 달려면 로그인하십시오.

 채택된 답변

Jan
Jan 2013년 1월 29일

1 개 추천

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

% --- Executes on button press in pushbutton5.
function pushbutton5_Callback(hObject, EventData, handles)
set(handles.pushbutton5, 'UserData', 0);
counter = get(hObject, 'UserData') + 1;
set(hObject, 'UserData', counter);
set(handles.text8, 'String', sprintf('%d', counter));
is what i wrote as the code. What do you mean adding into the 'opening function'
thanks
Thank you very much kind sir, it works. I'm just confused as to why I added
set(handles.pushbutton5, 'UserData', 0);
into opening function. What does that do for me?
Jan
Jan 2013년 1월 29일
It initialized the UserData property to 0. Otherwise the default is [].
Irwin2020
Irwin2020 2018년 11월 22일
편집: Irwin2020 2018년 11월 22일
Jan , can you please help me create a counter -loop adding strings (Hex values)?

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Azzi Abdelmalek
Azzi Abdelmalek 2013년 1월 25일

1 개 추천

set(handles.text8,'string',num2str(a));

댓글 수: 5

Amed
Amed 2013년 1월 25일
편집: Amed 2013년 1월 25일
converting a from a num to a string did not do it.
a = 0;
if (handles.pushbutton5)
a = a + 1;
set(handles.text8,'string',num2str(a));
end;
Azzi Abdelmalek
Azzi Abdelmalek 2013년 1월 25일
편집: Azzi Abdelmalek 2013년 1월 25일
Because every time you have
a=0
Use
a=str2double(get(handles.text8,'string'))
instead of
a=0
Amed
Amed 2013년 1월 25일
편집: Amed 2013년 1월 25일
yes i understand the code is wrong. so it reads as a = 0 every time. how do i allow some memory of sorts to be stored, so it knows to keep adding intervals of 1 to every click?
a=str2double(get(handles.text8,'string'));
if (handles.pushbutton5)
a = a + 1;
set(handles.text8,'string',a);
end;
I get NaN
Azzi Abdelmalek
Azzi Abdelmalek 2013년 1월 25일
Look at the above code
In opening function initialize
Set(handles.text8,'string','0')

댓글을 달려면 로그인하십시오.

Amed
Amed 2013년 1월 25일
편집: Amed 2013년 1월 25일

0 개 추천

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

Azzi Abdelmalek
Azzi Abdelmalek 2013년 1월 29일
편집: Azzi Abdelmalek 2013년 1월 29일
The initialization is not in the right place. You should do it in your opening function, not in your pushbutton function

댓글을 달려면 로그인하십시오.

Amed
Amed 2013년 1월 29일

0 개 추천

Anybody else have any ideas? Still not solved

댓글 수: 2

Jan
Jan 2013년 1월 29일
편집: Jan 2013년 1월 29일
Please post comments in the comment section and not as an answer. Thanks.
simran
simran 2013년 4월 28일
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.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

태그

질문:

2013년 1월 25일

편집:

2018년 11월 22일

Community Treasure Hunt

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

Start Hunting!

Translated by