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 件のコメント
Stephen23
2021 年 3 月 28 日
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
2021 年 3 月 28 日
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
2021 年 3 月 29 日
回答 (2 件)
Adam Danz
2021 年 3 月 28 日
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 件のコメント
Salil Joshi
2021 年 3 月 29 日
Salil Joshi
2021 年 3 月 29 日
Adam Danz
2021 年 3 月 29 日
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
2021 年 3 月 29 日
0 投票
4 件のコメント
Adam Danz
2021 年 3 月 29 日
That's cool! So many treasures in the FEX.
Salil Joshi
2021 年 3 月 29 日
Salil Joshi
2021 年 3 月 29 日
Adam Danz
2021 年 3 月 29 日
Did you download the file in that link and add it to your Matlab path?
カテゴリ
ヘルプ センター および File Exchange で Functions についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!