Clear Filters
Clear Filters

Updating the plot problem while using refreshdata

10 views (last 30 days)
Hi!! I was having a problem on my code while using the refreshdata, and I was hoping for your help. I want to see the actual first peak and valley from my data, then update the plot after passing them until i reach the last value in my data. I tried first to just run the whole data, but I am already stuck with it and just keep getting the whole data, and was fast too. I was actually planning to use it for the succeeding codes that I already have and for a real-time signal analysis.
I am not used to Matlab coding too so bear with me please.
Here is the code I made
%raw data from XYZ direction
NormChest = readtable('Tidal/TV_500ml.csv'); %%%get the raw data
C = table2array(NormChest(:,1));
x = table2array(NormChest(:,2));
y = table2array(NormChest(:,4));
z = table2array(NormChest(:,6));
total = table2array(NormChest(:,8));
time = C(:,1);
%--------------------------------------------------------------------------
% get relative acceleration
x = x - mean(x);
y = y - mean(y);
z = z - mean(z);
total = total - mean(total);
% % plot for raw data
% figure (1)
% plot (time, x, time, y, time, z, time, total), legend('x','y','z','total'); axis tight;
% title ("Subject 1 (500 mL)")
%--------------------------------------------------------------------------
%partial delay %Still having difficulty here
x1 = time; %trial
y1 = z; %trial
h = plot (x1, y1);
h.XDataSource = 'x1';
h.YDataSource = 'y1';
for i = 0:20
refreshdata (h, 'caller');
drawnow;
end
%
%
% for i = 1:1:numel (time)
%
% % refreshdata;
% end

Accepted Answer

Avni Agrawal
Avni Agrawal on 17 Jan 2024
Hi Stella,
I understand that you are trying to create a plot that updates in real-time, showing the first peak and valley of your data, and then updating the plot as you step through the data. The `refreshdata` function is used to update plots when the data source has changed, but in your case, you need to update the data source incrementally.
Here's a revised version of your code that will create an animated plot. This example uses a for-loop to simulate real-time data plotting by updating the plot with one data point at a time and then pausing briefly. This should give you the effect of the plot being drawn in real-time. Note that for large datasets, you may need to adjust the pause duration to control the speed of the animation.
% Read the data from the CSV file
NormChest = readtable('Tidal/TV_500ml.csv');
% Extract data from the table
time = NormChest{:, 1};
x = NormChest{:, 2} - mean(NormChest{:, 2});
y = NormChest{:, 4} - mean(NormChest{:, 4});
z = NormChest{:, 6} - mean(NormChest{:, 6});
total = NormChest{:, 8} - mean(NormChest{:, 8});
% Initialize the plot
figure(1);
h = plot(time(1), z(1), 'b'); % Start with the first data point
hold on; % Keep the plot for updating
xlabel('Time');
ylabel('Z-axis acceleration');
title('Subject 1 (500 mL) - Real-time Signal Analysis');
axis tight; % Set the axis limits to the range of the data
% Set the data source properties for dynamic updating
h.XDataSource = 'x1';
h.YDataSource = 'y1';
% Loop through the data to simulate real-time plotting
for i = 2:length(time)
% Update the data source
x1 = time(1:i);
y1 = z(1:i);
% Update the plot
refreshdata(h, 'caller');
drawnow;
% Pause for a brief moment to simulate real-time data acquisition
pause(0.01); % Adjust the pause duration as needed
end
hold off; % Release the plot
This code will create a figure and plot the first data point. Then, in the loop, it will incrementally add one data point at a time to the plot and refresh the figure with `refreshdata`. The `pause` function is used to slow down the loop to simulate real-time plotting. You can adjust the duration of the pause to match the speed of your actual data acquisition or desired visualization speed.
I hope this helps.
  1 Comment
Stella
Stella on 17 Jan 2024
Edited: Stella on 17 Jan 2024
Thank you Avni!!
I tried your code and it was great! But I also tried multiplying the i of x1 and y1 to 500 for getting every 500 data point, but I get this error "Index exceeds the number of array elements. Index must not exceed 29993. time appears to be both a function and a variable. If this is unintentional, use 'clear time' to remove the variable 'time' from the workspace."
I tried fixing my i value, but it still shows that error. I also tried just using the "time" data, but it cuts the plot short and only a quarter of the data was shown in the plot. Is there a command that I can use to fix it?
% Read the data from the CSV file
NormChest = readtable('Tidal/TV_500ml.csv');
% Extract data from the table
time = NormChest{:, 1};
x = NormChest{:, 2} - mean(NormChest{:, 2});
y = NormChest{:, 4} - mean(NormChest{:, 4});
z = NormChest{:, 6} - mean(NormChest{:, 6});
total = NormChest{:, 8} - mean(NormChest{:, 8});
%design for low pass filter
fs = 1000; %sampling frequency
fc = 1; %cut-off frequency
order = 1;
[b1, a1] = butter (order, fc/(fs/2));
%design for high pass filter
fcv = 0.1;
orderv = 1;
[b2, a2] = butter (orderv, fcv*2/fs, "high");
%--------------------------------------------------------------------------
%Filtering
acc = z
acc = filtfilt (b1, a1, acc);
% Initialize the plot
figure(2);
h = plot(time(1), acc(1)); hold on; % Start with the first data point (1)
xlabel('Time'); ylabel('y-axis acceleration'); axis tight;
title('Subject 2 (1.5 L) - Real-time Signal Analysis');
h.XDataSource = 'x1'; % Set the data source properties for dynamic updating
h.YDataSource = 'y1';
for i = 2:length (time)
% Update the data source
x1 = time(1:i*500);
y1 = acc(1:i*500);
refreshdata(h, 'caller');
drawnow;
pause(0.001); % Can be adjusted
end
hold off;

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Performance in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!