Getting current time on x-axis using Design app - GUI

15 次查看(过去 30 天)
Hello! I'm trying to use GUI app to plot real-time data and I can only get the x-axis to show the elapsed time in seconds, but I would like to get the current clock time, like 13:48:17. I've tried a few things but nothing seems to work, I would really appreciate your help. Also I would like the plot to update every 1sec rather than constantly, but don't know if that is defined on the hGui function or on the dataCapture function.
I'm using the code in this page https://www.mathworks.com/help/daq/software-analog-triggered-data-capture.html, and I think I've got to edit the hGui function somewhere in this part:
hGui.Axes1 = gca;
hGui.LivePlot = plot(0, zeros(1, numel(s.Channels)));
xlabel('Time (s)');
ylabel('Voltage (V)');
title('Continuous data');
legend({s.Channels.ID}, 'Location', 'northwestoutside')
hGui.Axes1.Units = 'Pixels';
hGui.Axes1.Position = [207 391 488 196];
% Turn off axes toolbar and data tips for live plot axes
hGui.Axes1.Toolbar.Visible = 'off';
disableDefaultInteractivity(hGui.Axes1);
This is how I'm acquiring data:
[eventData, eventTimestamps] = read(src, src.ScansAvailableFcnCount,'OutputFormat', 'Matrix');
If I could change evenTimestamps to the current time in the format 'HH:MM:SS', I think that would do it, because in the matrix instead of the miliseconds I would be getting the current time.
Or maybe I've got to edit the dataCapture function in this part:
% Update x-axis limits
xlim(hGui.Axes1, [dataBuffer(firstPoint,1), dataBuffer(end,1)]);
% Live plot has one line for each acquisition channel
for ii = 1:numel(hGui.LivePlot)
set(hGui.LivePlot(ii), 'XData', dataBuffer(firstPoint:end, 1), ...
'YData', dataBuffer(firstPoint:end, 1+ii))
end
It may not have anything to do with editing either of this code parts so I would really like someones help :)
  2 个评论
Geoff Hayes
Geoff Hayes 2021-7-2
Teresa - so you want to show the current time along the x-axis? Where is the time data that corresponds to the buffered data?

请先登录,再进行评论。

采纳的回答

Cris LaPierre
Cris LaPierre 2021-7-2
The same way you would do it in MATLAB. If your duration corresponds to daytime, then you can just set the format of your xdata using xtickformat.
x=seconds(4800:50:5900);
y = rand(size(x));
plot(x,y)
xtickformat('hh:mm:ss')
  24 个评论
Cris LaPierre
Cris LaPierre 2021-7-8
Not perfect, but this works for what I was trying to create. You will likely have to modify to get it to do exactly what you want.
Main changes:
1. in createDataCaptureUI, create the plot using a time for your x value instead of 0. I used datetime("now"). You'll want to add this value to hGui so it is available to adjust your time data in dataCapture. This is where you set xtickformat as well. Here is just the subset with all 3 changes.
...
hGui.t0 = datetime("now");
% Create the continuous data plot axes with legend
% (one line per acquisition channel)
hGui.Axes1 = axes;
hGui.LivePlot = plot(hGui.t0, zeros(1, numel(s.Channels)));
xlabel('Time (s)');
ylabel('Voltage (V)');
title('Continuous data');
xtickformat('HH:mm:ss');
...
2. In dataCapture, I've assumed your XData is elapsed time in seconds. To make it day time, I need to convert the first column of dataBuffer to seconds and add hGui.t0 to it. The xlim command is setting what data is displayed. I don't know what you want, so I set it to just show the new data. You can modify this to meet your requirements.
...
% Update x-axis limits
xlim(hGui.Axes1, hGui.t0 + seconds(dataBuffer([firstPoint end], 1)'));
% Live plot has one line for each acquisition channel
for ii = 1:numel(hGui.LivePlot)
hGui.LivePlot(ii).XData = hGui.t0 + seconds(dataBuffer(firstPoint:end, 1));
hGui.LivePlot(ii).YData = dataBuffer(firstPoint:end, 1+ii);
end
...
I left all other code the same. Here is the resulting plot in the gui.
My value for hGui.t0 was 08-Jul-2021 13:23:43 and the diatafile you shared had ~6.8 seconds of data collected at a rate of 1000 scans/second.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Graphics Performance 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by