Automatically update a string in GUIDE textbox
Mostrar comentarios más antiguos
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!
Respuestas (2)
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 comentario
Jeffrey Alido
el 21 de Jul. de 2017
Image Analyst
el 21 de Jul. de 2017
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 comentarios
Jeffrey Alido
el 25 de Jul. de 2017
Image Analyst
el 25 de Jul. de 2017
Are you sure handles.text2 is a valid control on your GUI, and that handles has a good value for it? Set a breakpoint on the tic line, and type
>> handles.text2
on the command line and see what it reports.
Also, run the attached demo.
Jeffrey Alido
el 28 de Jul. de 2017
Image Analyst
el 28 de Jul. de 2017
That's a lot trickier since the other GUI doesn't have the handle. I'd recommend you make just one comprehensive GUI than a bunch of floating, separate, overlapping GUIs that will clutter up your desktop. If you insist, see near the end of this FAQ entry: http://matlab.wikia.com/wiki/FAQ#How_can_I_share_data_between_callback_functions_in_my_GUI.28s.29.3F. I don't like that approach so I haven't done it, so I can't help you if you want to go down that path.
Walter Roberson
el 28 de Jul. de 2017
Jeffrey did not ask about a separate GUI, only a separate .m file. As long as the function being called has access to some object that is within the main GUI, then guidata() can retrieve the handles structure, and then whatever fields desired can be accessed.
Image Analyst
el 28 de Jul. de 2017
Oh, okay. It's easiest if the second m-file is a function and then you can just pass handles of your first GUI into your second m-file function. So if your first GUI can start running your second function, then you can do (in your fist program)
% Call the function "secondMFile" passing it "handles" from your first GUI m-file
secondMFile(handles);
If your second m-file was started by you or some other program, then you'll have to use things like assignin() and evalin() I believe. Again, I never do that.
Jeffrey Alido
el 28 de Jul. de 2017
Image Analyst
el 28 de Jul. de 2017
I'd rather not give code for an approach that I don't recommend.
Categorías
Más información sobre Scripts en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!