Dynamic text like current time in prompt

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
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
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
Salil Joshi le 29 Mar 2021
Thanks Adam and Stephen (not sure why it is not letting me reply to Stephen though). I essentially want to display the current time or the time elapsed while waiting for the input, so that the user gets an idea of what stage the data acquisition is at. If I use the input function, then matlab waits for my input, so it cannot cycle back to keep updating the prompt.

Connectez-vous pour commenter.

Réponses (2)

Adam Danz
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
Salil Joshi le 29 Mar 2021
Wow! Thanks! Excuse me while I go back and try to understand this code. Me sees lot of learnings in there!
:-)
Salil Joshi
Salil Joshi le 29 Mar 2021
I essentially want to display the current time or the time elapsed while waiting for the input, so that the user gets an idea of what stage the data acquisition is at. If I use the input function, then matlab waits for my input, so it cannot cycle back to keep updating the prompt.
Adam Danz
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?

Connectez-vous pour commenter.

Walter Roberson
Walter Roberson le 29 Mar 2021

0 votes

4 commentaires

Adam Danz
Adam Danz le 29 Mar 2021
That's cool! So many treasures in the FEX.
Salil Joshi
Salil Joshi le 29 Mar 2021
Thanks a bunch!
Salil Joshi
Salil Joshi le 29 Mar 2021
I am on R2020b, and it is whining about Unrecognized function or variable 'setPrompt'. Not sure if it needs some specific packages installed.
Adam Danz
Adam Danz le 29 Mar 2021
Did you download the file in that link and add it to your Matlab path?

Connectez-vous pour commenter.

Catégories

Produits

Commenté :

le 29 Mar 2021

Community Treasure Hunt

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

Start Hunting!

Translated by