Dynamic text like current time in prompt
Afficher commentaires plus anciens
Suppose I have this code like:
t=input(sprintf('%s\tInput temperature: ',datetime))
or
t_start=tic;
t=input(sprintf('%s\tInput temperature: ',seconds(toc(t_start))))
How do I get the datetime or the time elapsed to update continuously every second or whatever?
Can the input or the result of something like display("random text") be updated?
Thanks!
3 commentaires
Stephen23
le 28 Mar 2021
In theory you could use backspace to overwrite a line of text (which does not include a newline), but most likely you would be much better off using a proper GUI.
Adam Danz
le 28 Mar 2021
Once input(__) is executed it can only be escaped by pressing the return key or ctrl+c and the prompt cannot be changed. You can use a GUI along with a timer.
Salil Joshi
le 29 Mar 2021
Réponses (2)
Adam Danz
le 28 Mar 2021
Here's a custom dialog box that requests input from the user and displays waiting time in seconds in the upper left corner. Timing stops when the figure is closed or when enter is pressed and the timing (duration) and user input (response) are returned.
Be cautious of the timer and tic/tic timing if you need a fine precision of temporal resolution.

% Build GUI (it will become visible at the end)
h.fig = figure('Position', [50 50 400 200],'Visible','off');
movegui(h.fig, 'center')
h.text = uicontrol(h.fig, 'Style', 'text', 'String', 'Enter something',...
'Units','normalize', 'Position', [0,.75,1,.1], 'HorizontalAlignment', 'center',...
'FontSize', 12);
h.inputbox = uicontrol(h.fig, 'Style', 'edit', 'Units', 'Normalize', ...
'Position', [.2 .35 .6, .25],'FontName', 'FixedWidth','FontSize', 10);
h.button = uicontrol(h.fig, 'Style', 'pushbutton', 'String','Enter', ...
'Units','Normalize','Position',[.4 .1 .2, .15], 'callback', @(h,~)uiresume(ancestor(h,'figure')));
h.timeStr = uicontrol(h.fig, 'Style', 'text', 'String', '0 sec',...
'Units','normalize', 'Position', [.05,.75,.15,.1], 'HorizontalAlignment', 'center',...
'FontName','FixedWidth','FontSize', 10);
% Create timer that updates the waiting time
h.timer = timer();
h.timer.ExecutionMode = 'fixedRate';
h.timer.Period = 1; % seconds
h.timer.StartDelay = 1;
h.timer.TimerFcn = @(~,~)set(h.timeStr,'String',regexprep(h.timeStr.String,'^\d+','${num2str(str2double($0)+1)}'));
h.timer.StopFcn = @(timerHandle,~)delete(timerHandle);
h.fig.UserData.cleanup = onCleanup(@()stop(h.timer));
% turn on GUI, start timer, and wait for user
h.fig.Visible = 'on';
start(h.timer)
starttime = tic();
uiwait(h.fig)
% % % % % % % % % % % % % % % % % %
% %
% Exectution is suspended until %
% user closes figure or presses %
% enter. %
% %
% % % % % % % % % % % % % % % % % %
% Get waiting time
duration = toc(starttime); %seconds
% Get user's response
if isvalid(h.fig)
% User pressed enter
response = h.inputbox.String;
close(h.fig) % close GUI
else
% User closed fig
response = '';
end
% Show results
fprintf('Wait time: %.2f sec. Response: %s\n', duration, response)
3 commentaires
Salil Joshi
le 29 Mar 2021
Salil Joshi
le 29 Mar 2021
Adam Danz
le 29 Mar 2021
Exactly.... But see the link Walter Roberson shared.
The GUI I threw together shows the elapsed time. It can easily be changed to show the current time or a different type of duration. What are you imagining?
Walter Roberson
le 29 Mar 2021
0 votes
4 commentaires
Adam Danz
le 29 Mar 2021
That's cool! So many treasures in the FEX.
Salil Joshi
le 29 Mar 2021
Salil Joshi
le 29 Mar 2021
Adam Danz
le 29 Mar 2021
Did you download the file in that link and add it to your Matlab path?
Catégories
En savoir plus sur Downloads dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!