How can I plot a multidimensional array?

Hello, I want to create a gif from the data inside a 32x360x360 double array. The thing is I want to plot the 32 values for each 360x360, I mean plotting first 32x1x1, then 32x1x2, 32x1x3, 32x1x4, ..., 32x1x360, 32x2x1, 32x2x2, and so on making a gif until it reaches 32x360x360. Can this be done?
The next code is one I have if the vector was 32x129600. How can I change it to plot the other vector of one more dimension without reshaping it? As I would like to have a title for the gif with something like i = 1,2,3,4,...360 and j=1,2,3,4,...360 to see how it changes.
for i=1:JN
plot(Vector(:,i), 'k-', 'LineWidth', 2);
axis([startpoint finishpoint miny maxy])
xlabel('Time(s)')
ylabel('Relative Brightness')
grid on
% Capture the plot as an image
frame = getframe(h);
im = frame2im(frame);
[imind,cm] = rgb2ind(im,256);
% Write to the GIF File
if i == 1
imwrite(imind,cm,filename,'gif', 'Loopcount',inf,'DelayTime',0.1);
else
imwrite(imind,cm,filename,'gif','WriteMode','append','DelayTime',0.1);
end
end

 Accepted Answer

Make JN = (360*360) .
When you index with fewer indices than there are dimensions, then the last index acts like a linear index relative to that dimension and later. So Vector(:,360) would correspond to Vector(:,360,1) and Vector(:,361) would correspond to Vector(:,2,1)

7 Comments

Thank you so much and now if I wanted a title for my plot that changes over time. There are two angles: theta: 0-360º and phi: 0-360º. The array is then 32x360x360 (32xthetaxphi), so how can I put a title that changes first the theta going from 0 to 360 and every time it reaches 360, the phi increases 1 and the theta starts again.
title(['Theta = ' num2str(i) 'º, Phi = ' num2str(i) 'º and Alpha = 0º'])
For example with that one I can only increase both number until they get to 360x360 but I want that theta increases until 360 and then phi increases 1. Like:
Theta = 1º, Phi = 1º ----> Theta = 2º, Phi = 1º ----> Theta = 360º, Phi = 1º ----> Theta = 1º, Phi = 2º and so on.
theta = floor((i-1)/360);
phi = mod(i - 1, 360);
title( sprintf('Theta = %dº, Phi = %dº and Alpha = 0º', theta, phi));
Note: it is not clear from your question whether theta increases down the columns or across the rows. It is also not clear whether you want 0:1:359 or 1:1:360 .
Also, I notice that you had asked 32x1x1, then 32x1x2, 32x1x3, 32x1x4 but that the technique I provided earlier would go (:,1,1) then (:,2,1) then (:,3,1) and so on. You would want to permute(Vector,[1 3 2]) to access it in the order you are wanting. Or use a double nested loop.
I would suggest to you that a double nested loop would be clearer code. Any efficiency you might gain by using a single loop is negated by having to calculate the theta and phi based upon a single index.
Thank you. The data increases along the rows, I mean, each column has 32 points that are the plot of one curve, so I plot each time one curve, which would correspond to a value of theta and phi. Then the next plot would be the 32 points of theta = 2º and phi = 1º, then theta = 3º and phi = 1º and so on. And I want it like 1:1:360.
And sorry I meant 32x1x1, then 32x2x1, 32x3x1 so your technique is the one I needed. The array 32x360x360 is 32xthetaxphi so I need first to run the theta values until 360 so phi increases in one.
And how could I do a double nested loop? Sorry I'm quite stuck with this.
ax = gca;
first == true;
for theta = 1:360
for phi = 1:360
if first
h = plot(Vector(:,theta, phi), 'k-', 'LineWidth', 2);
axis(ax, [startpoint finishpoint miny maxy])
xlabel(ax, 'Time(s)')
ylabel(ax, 'Relative Brightness')
grid(ax, 'on')
else
set(h, 'YData', Vector(:,theta,phi))
end
title(ax, sprintf('Theta = %dº, Phi = %dº and Alpha = 0º', theta, phi));
drawnow()
% Capture the plot as an image
frame = getframe(ax);
im = frame2im(frame);
[imind,cm] = rgb2ind(im,256);
% Write to the GIF File
if first
imwrite(imind,cm,filename,'gif', 'Loopcount',inf,'DelayTime',0.1);
else
imwrite(imind,cm,filename,'gif','WriteMode','append','DelayTime',0.1);
end
first = false;
end
end
Thank you so much. The only problem it is changing firstly the phi and after the theta but I suppose it is just changing the two angles at the beginning. But the rest it's perfect.
And the last thing, if I wanted to make a video instead of a gif from the figures how could this be done?
You can exchange to for phi then for theta without changing the rest.
Perfect, thank you so much.

Sign in to comment.

More Answers (0)

Products

Release

R2018b

Community Treasure Hunt

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

Start Hunting!