필터 지우기
필터 지우기

Automatically update a string in GUIDE textbox

조회 수: 4 (최근 30일)
Jeffrey Alido
Jeffrey Alido 2017년 7월 21일
댓글: Image Analyst 2017년 7월 28일
I would like to be able to press a button and have the text field of a static textbox automatically update as a variable changes within a function.
I have some timer function:
function stopwatch
global t
for i = 1:10
clc;
t = tic
pause(1)
end
and the GUI callback function for the pushbutton:
function pushbutton1_Callback(hObject, eventdata, handles)
stopwatch
global t
set(handles.text2,'string',t)
but the textbox only updates once the loop is finished. How do I make it so that the textbox is updated automatically continuously?
Thanks!

답변 (2개)

Stephen23
Stephen23 2017년 7월 21일
편집: Stephen23 2017년 7월 21일
You will need to set the text string inside the loop. For example (untested):
function stopwatch(h)
for k = 1:10
pause(1);
set(h,'string',tic)
end
end
function pushbutton1_Callback(hObject, eventdata, handles)
stopwatch(handles.text2)
or even all in one function:
function pushbutton1_Callback(~, ~, handles)
for k = 1:10
pause(1);
set(handles.text2,'string',tic)
end
end
  댓글 수: 1
Jeffrey Alido
Jeffrey Alido 2017년 7월 21일
Is there a way to do it while keeping the stopwatch function in a separate .m file?

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


Image Analyst
Image Analyst 2017년 7월 21일
Have stopwatch take handles as an input and set it in there and call drawnow:
global t
for i = 1:10
clc;
t = tic
handles.text2.String = sprintf('Starting Time = ', t);
drawnow;
pause(1)
end
Not sure that makes sense to use tic though. You're starting 10 timers but saving only the starting time of the very last timer. Why????
  댓글 수: 8
Jeffrey Alido
Jeffrey Alido 2017년 7월 28일
I'm still a beginner so I am unsure what the code would all look like. Could you help from here?
So far I have my timer.m function:
function timer
for i = 1:15
clc;
i
pause(1)
end
And I'd like to press a pushbutton in the GUI so that a static textbox displays the timer live.
Thanks!
Image Analyst
Image Analyst 2017년 7월 28일
I'd rather not give code for an approach that I don't recommend.

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

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by