Creating a counter loop in MatLab, GUI?
조회 수: 15 (최근 30일)
이전 댓글 표시
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
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
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
2013년 1월 29일
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));
댓글 수: 4
추가 답변 (3개)
Azzi Abdelmalek
2013년 1월 25일
set(handles.text8,'string',num2str(a));
Amed
2013년 1월 25일
편집: Amed
2013년 1월 25일
댓글 수: 1
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!