Subplots using for loop with varying data increments

Hi Friends,
I am reading the following data
d=xlsread('data.xlsx','sheet1')
t=d(:,1:4:5);
a=d(:,2:4:6);
v=d(:,3:4:7);
u=d(:,4:4:8);
how can I subplot this data to a 2x3 figures,
1 2 3
4 5 6
figs in Col1: 1:3:4 (1, 4) are with t, and a
figs in Col2: 2:3:5 (2, 5) are with t, and v
figs in Col3: 3:3:6 (3, 6) are with t, and u
Currently, I can plot them individually subplot but worried to making a mistake while creating each subplot in large data. Appreciate any ideas.
Thanks,
Jagan

2 Kommentare

What would be the difference between figure 1 an 4 ?
it is like
subplot(2, 3, 1)
plot (d(:,1), d(:,2)) % defined them again as t, a, v, u, which must now be plot (t(:,1),a(:,1))
subplot (2,3,2)
plot(d(:,1),d(:,3))
subplot(2,3,3)
plot(d(:,1),d(:,4))
subplot(2,3,4)
plot(d(:,5),d(:,6))
subplot(2,3,5)
plot(d(:,5),d(:,7))
subplot(2,3,6)
plot(d(:,5),d(:,8))

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Walter Roberson
Walter Roberson am 27 Apr. 2020
plotvars = {a, v, u};
for K = 1 : 3
subplot(2, 3, K)
plot(t, plotvars{K});
subplot(2, 3, K+3)
plot(t, plotvars{K});
end

12 Kommentare

Hi mate,
with the above loop, the output shows multiple data in a single subplot.
Like subplots 1,4 showing data from both 1, 4. Likewise (2,5), and (3,6)
attached is the output generated. But I am looking to plot as attached in the previous comment box.
JAGAN, do you just want to make 2 rows or multiple rows as you showed in the comment to your question?
Hi Ameer,
In the figure I had more data, but for the sake of question I asked, it is just 2x3 subplots. It this works for 2x3, it would work for 8x3 was my thought.
Thanks.
Ameer Hamza
Ameer Hamza am 27 Apr. 2020
Bearbeitet: Ameer Hamza am 27 Apr. 2020
There can be several methods to do a thing, and it is not necessary that a code that works for 2 cases can be trivially extended to several cases. Walter's code can be adapted to a general case like this. It takes a group of 4 columns, takes the first column as x-coordinates, and other 3 columns as y-coordinates, and then moves to the next four columns.
d = xlsread('data.xlsx','sheet1')
line_colors = {'b', 'k', 'r'};
line_widths = {0.5, 1.5, 1};
x_limits = {[0 15], [0 15], [0 15]};
xCol_idx = 1:4:size(d,2);
count = 1;
num_rows = size(d,2)/4;
for i=1:numel(xCol_idx)
for j=1:3
subplot(num_rows, 3, count);
plot(d(:,xCol_idx(i)), d(:,xCol_idx(i)+j), ...
'Color', line_colors{j}, ...
'LineWidth', 2);
xlim(x_limits{j});
count = count + 1;
end
end
JAGAN MOHAN KUMMARI
JAGAN MOHAN KUMMARI am 27 Apr. 2020
Bearbeitet: JAGAN MOHAN KUMMARI am 27 Apr. 2020
Hi Ameer,
I havent yet tried this one. As I understood, the above script groups 4 columns and subplots simultaneously.
However, another situation is, subplots in col1 will have different axis limits, and like wise subplots in col2 and col3, along with other properties like line width, color etc.,
I have done manual subplots and added the information I needed, as attached in the figure.
Thank you for your time.
See the updated code in my comment. It allows you to set different color, linewidths and xlimits for each column of subplot. The code gives a general template. You can modify it add whatever customization you want.
This works great mate. Learned the new way of doing this. I can do some customization.
Thank you for helping this out. Highly appreciate!
Kind regards,
I am glad to be of help.
Hi Ameer,
How can I assign axis limits only to one or two columns, and not to other?
y_limits={[-0.5 0.5],[-300 300],[-500 900]};
in this case, I would like to leave the thrid column to default, as the limits are sometimes high compared to others which are of importance as well.
Thank you.
I got it.
y_limits={[-0.5 0.5],[-300 300],'auto'};
this worked and can use it for any column.
Thanks again mate.
Similarly, this will also work
y_limits={[-0.5 0.5],[-300 300],[-inf inf]};
True, works the same.
Thanks mate!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by