plotting array of arrays

8 次查看(过去 30 天)
shannon Ross
shannon Ross 2011-4-5
I need to figure out how to plot data from an array of arrays. Below is an example setup of data I am having to process. In reality I had to import data in sections to avoid reaching the maximum array size limit.
Basically I know how to plot the data if I use cell2mat, however doing this will generate the max array size error.
Does anyone know how to plot the data and generate the same graph shown in the example without compiling the data into one continuous array?
%===============================================
% Creating example time array
% for simplicity, it is only increasing numbers
%===============================================
% an array of increasing numbers is created
% this simulates an increase in time
x = linspace(1, 100, 100)';
%create and array of arrays
arrayTime{1,1}=x;
arrayTime{2,1}=x+100;
arrayTime{3,1}=x+200;
arrayTime{4,1}=x+300;
%===============================================
% Creating example data array
%===============================================
y = [linspace(20, 25, 100)' (linspace(20, 25, 100)')+10 (linspace(20, 25, 100)')+15];
z = rand(100,3);
w = y + z;
arrayData{1,1}=w+5;
arrayData{2,1}=w+10;
arrayData{3,1}=w+15;
arrayData{4,1}=w+20;
%===============================================
% Plotting array data by combining data
%===============================================
Time = cell2mat(arrayTime);
Data = cell2mat(arrayData);
hold on
plot(Time,Data(:,1),'-b');
plot(Time,Data(:,2),'-g');
plot(Time,Data(:,3),'-r');
hold off
legend('data set 1','data set 2','data set 3');

回答(3 个)

Laura Proctor
Laura Proctor 2011-4-5
If I understand correctly, you want to plot without combining all of the data into one array. Replacing the last bit of code with the following should achieve this goal.
hold on
for idx = 1:4
plot(arrayTime{idx,1},arrayData{idx,1}(:,1),'-b')
plot(arrayTime{idx,1},arrayData{idx,1}(:,2),'-g')
plot(arrayTime{idx,1},arrayData{idx,1}(:,3),'-r')
end
hold off
legend('data set 1','data set 2','data set 3');

Walter Roberson
Walter Roberson 2011-4-5
hold on
for K = 1 : length(arrayTime)
t = arrayTime{K}
d = arrayData{K};
plot(t, d(:,1), '-b', t, d(:,2), '-g', t, d(:,3), '-r');
end
for K = 1: length(ArrayTime) - 1
t = [arrayTime{K}(end) arrayTime{K+1)(1)];
d = [arrayData{K}(end,1:3); arrayData{K+1}(1,1:3)];
plot(t, d(:,1), '-b', t, d(:,2), '-g', t, d(:,3), '-r');
end
This code plots in chunks and then goes back and adds lines joining the chunks. Other approaches are possible.

shannon Ross
shannon Ross 2011-4-5
Thanks for the quick response, both work. Just fixed some errors in the second one.
hold on
for K = 1 : length(arrayTime)
t = arrayTime{K}
d = arrayData{K};
plot(t, d(:,1), '-b', t, d(:,2), '-g', t, d(:,3), '-r');
end
for K = 1: length(arrayTime) - 1
t = [arrayTime{K}(end) arrayTime{K+1}(1)];
d = [arrayData{K}(end,1:3); arrayData{K+1}(1,1:3)];
plot(t, d(:,1), '-b', t, d(:,2), '-g', t, d(:,3), '-r');
end
hold off
legend('data set 1','data set 2','data set 3');

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by