How to create multiple plots for time series data?

Hi everyone!
Could someone help me with the following task? - I've got a matrix (example below), where the first column acts as a counter and occasionally ends randomly and starts off with a 1 again. I need to plot each column (except column 1) with a separate plot every time the counter starts at 1. So in the example below, I would plot columns 2,3,4 and 5 every time column 1 is 1 - rows 1 to 8 and then a new plot for each column rows 1 to 9.
1 0.170000000000000 -0.0480000000000000 -0.119000000000000 -0.0770000000000000
2 -0.00400000000000000 -0.187000000000000 -0.362000000000000 -0.333000000000000
3 -0.896000000000000 -1.01400000000000 -1.34500000000000 -1.20700000000000
4 -2.25100000000000 -2.10300000000000 -2.93200000000000 -2.57000000000000
5 -3.61200000000000 -3.31500000000000 -4.20000000000000 -3.97900000000000
6 -4.56500000000000 -4.53900000000000 -5.16900000000000 -4.85300000000000
7 -5.32800000000000 -5.52400000000000 -5.72600000000000 -5.59600000000000
8 -5.60500000000000 -6.22900000000000 -6.30000000000000 -6.18100000000000
1 0.269000000000000 0.0630000000000000 0.0230000000000000 0.114000000000000
2 0.238000000000000 0.0330000000000000 -0.300000000000000 0.138000000000000
3 -0.682000000000000 -0.954000000000000 -1.24300000000000 -1.07300000000000
4 -1.95300000000000 -2.34300000000000 -2.69200000000000 -2.58000000000000
5 -3.15600000000000 -3.76700000000000 -3.98900000000000 -4.06400000000000
6 -4.33200000000000 -5.05900000000000 -5.10500000000000 -5.17300000000000
7 -5.21500000000000 -5.85000000000000 -5.71100000000000 -5.97200000000000
8 -5.78300000000000 -6.22900000000000 -6.15600000000000 -6.45500000000000
9 -6.00500000000000 -6.59700000000000 -6.44900000000000 -6.96900000000000

 Akzeptierte Antwort

Sindar
Sindar am 7 Jul. 2020
Bearbeitet: Sindar am 7 Jul. 2020
While you may want to think about a cleaner way of dealing with the plot (e.g., plotting in the same figure window and saving), this'll do what you need:
% find the index of all the 1's in the first column
idx = find(data_counter(:,1)==1);
% loop over the sets
for ind_set=1:length(idx)
% figure out where the current set starts and ends
% you already know where it starts
start_ind = idx(ind_set);
% if this is the last set, it ends at the last row
if ind_set==length(idx)
end_ind = size(data_counter,1);
% otherwise, it ends one row before the next set starts
else
end_ind = idx(ind_set+1)-1;
end
% loop over the columns
for ind_col = 2:size(data_counter,2)
% option A: open a new figure so nothing gets overwritten
figure
% plot the correct slice of data
plot(data_counter(start_ind:end_ind,ind_col))
% option B: save each plot as it is created
print("Fig__set-"+ind_set+"_col-"+ind_col,'-dpng')
end
end

3 Kommentare

Thank you very much, it works beautifully.
You are correct to suggest that plotting all in one figure would be much wiser.
Sindar
Sindar am 7 Jul. 2020
I added a line to the solution for that. Comment out the figure line to stop new windows opening
Thank you, that's very helpful. :)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Community Treasure Hunt

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

Start Hunting!

Translated by