I'm trying to plot a number over time. The idea is to show on X-axis the date with milliseconds format.
The code below is not working properly, could some one help me?
figure(1)
while(1)
timestr = datetime(clock,'InputFormat','yyyy-MM-dd HH:mm:ss.S', 'Format', 'yyyy-MM-dd HH:mm:ss.SSS');
plot(timestr, rand(1));
pause(0.01)
end

 채택된 답변

Jon
Jon 2019년 10월 11일
편집: Jon 2019년 10월 11일

1 개 추천

I'm not quite clear on what you are trying to accomplish but it looks like some how you want to see the plot continue to evolve as time goes along.
You could modify your code as follows and see the points continuously updated, the key is to use the hold on command:
% assign number of iterations
numIter = 1000;
% open new figure
figure
hold on % so figure isn't cleared with each plot command
% initialize previous values for connecting the dots
tOld = datetime('now');
yOld = rand(1);
% enter plotting loop
for k = 1:numIter
t= datetime('now');
y = rand(1);
% plot data up to the current iteration
plot([tOld,t], [yOld,y],'-bo');
xtickformat('yyyy-MM-dd HH:mm:ss.SSS')
tOld = t;
yOld = y;
pause(0.01)
end
hold off
Note, I made the code loop for a fixed number of iterations rather than have to ctrl-c to end the infinite while loop that you had.
Also a few other cleanup items, the clock command returns a six element date vector. If you call datetime(clock) it must first convert the clock value to a datetime. Instead just use datetime('now'). There is no need for formatting the value returned by calling datetime('now'), that is handled by the xtickformat
In the above code I connected the data points with lines. If you don't need the dots connected by lines then you don't need to save the previous values, you can just plot t and y with a symbol

댓글 수: 4

Eduardo Santos
Eduardo Santos 2019년 10월 11일
편집: Eduardo Santos 2019년 10월 11일
Thanks my friend. :)
The problem was solved partially. Unfortunately I was trying to implement this on app designer but there is no xtickformat property :(
Jon
Jon 2019년 10월 11일
I just tried this myself in app designer. There is no problem using the xtickformat property in app designer, but you must specify the axes that you want to apply it to. Here is an example where I have have a user interface with just an axis and then a button push to plot something versus time. Here I just show the callback for the button push function
% Button pushed function: Button
function ButtonPushed(app, event)
t(10) = datetime('now')
y = zeros(10,1)
for k = 1:10
t(k) = datetime('now')
y(k) = rand
pause(0.01)
end
plot(app.UIAxes,t,y)
xtickformat(app.UIAxes,'yyyy-MM-dd HH:mm:ss.SSS')
end
end
Eduardo Santos
Eduardo Santos 2019년 10월 11일
Wow.
Thanks again. That was exactly I was looking for!
Jon
Jon 2019년 10월 11일
Excellent! Glad that is working for you.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Dates and Time에 대해 자세히 알아보기

질문:

2019년 10월 11일

댓글:

Jon
2019년 10월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by