Subplots of multidimensional arrays in Matlab - matlab

I have a 10x10x10 array, z. How do I plot everything in the SAME window so that I would have the 3 plots for z(:,:,1) and below it the three plots for z(:,:,2) etc.?
This is what I have so far:
for i = 1:10
z=z(:,:,i);
figure(i)
subplot(1,2,1)
surf(z)
%code, obtain new array called "new1"...
subplot(1,2,2)
surf(new1)
%code, obtain new array called "new2"...
subplot(1,3,3)
surf(new2)
end;

I think the first two subplots are supposed to be subplot(1,3,1) and subplot(1,3,2). Also, try inserting hold on after each subplot command --- this should allow you to keep whatever has been plotted before.
for i = 1:10
z=z(:,:,i);
figure(i)
subplot(1,3,1)
hold on;
surf(z)
%code, obtain new array called "new1"...
subplot(1,3,2)
hold on;
surf(new1)
%code, obtain new array called "new2"...
subplot(1,3,3)
hold on;
surf(new2)
end;

What is new1 and new2? Are they the same for all rows? Or also 3D arrays?
I think you need something like this:
for i = 1:10
subplot(10*3,3,(i-1)*3+1)
surf(z(:,:,i))
subplot(10*3,3,(i-1)*3+2)
surf(new1)
subplot(10*3,3,(i-1)*3+3)
surf(new2)
end
Or more generally for variable size of z:
N = size(z,3);
for i = 1:N
subplot(N*3,3,(i-1)*3+1)
surf(z(:,:,i))
subplot(N*3,3,(i-1)*3+2)
surf(new1)
subplot(N*3,3,(i-1)*3+3)
surf(new2)
end

Related

Update plot using hold on inside a for loop

I'm combining two plots using this code
plot(x1,y1,'.','MarkerSize',20,'Color','r');
hold on; grid on;
plot(x2,y2,'x','MarkerSize',10,'Color','b');
xlim([-a a]);
ylim([-a a]);
Now I want to change the values of x1,y1 and x2,y2 in order to have more than one point and one cross inside my figure. I tried to use a for loop where I compute new values, but every iteration this code generates another figure - whereas I want just one figure with all the points in it.
I did something like this:
for i=1:1:8
% do things that compute x1,x2,y1,y2
figure; hold on
plot(x1,y1,'.','MarkerSize',20,'Color','r');
hold on; grid on;
plot(x2,y2,'x','MarkerSize',10,'Color','b');
xlim([-a a]);ylim([-a a]);
i=i+1;
end
I also tried to put the hold on just before i=i+1 but still give me a new figure.
There are several things you can do:
The simple solution would be to put the figure command outside the loop:
figure(); hold on;
for ...
plot(x1, ...);
plot(x2, ...);
end
A better solution would be to first compute all values and then plot them:
[x1,y1,x2,y2] = deal(NaN(8,1));
for ind1 = 1:8
% do some computations
x1(ind1) = ...
...
y2(ind1) = ...
end
figure(); plot(x1,y1,'.',x2,y2,'x');
The best solution (in my opinion) would be to update existing plot objects with new data points as they become available:
[x1,y1,x2,y2] = deal(NaN(8,1));
figure(); hP = plot(x1,y1,'.',x2,y2,'x');
for ind1 = 1:8
% do some computations
hP(1).XData(ind1) = <newly-computed x1>
hP(1).YData(ind1) = <newly-computed y1>
hP(2).XData(ind1) = <newly-computed x2>
hP(2).YData(ind1) = <newly-computed y2>
end

Handling and eliminating multiples entries in MatLab legend

I currently want to have the legend of graph, however i'm plotting several lines that should be group in only 3 types.
My currently option is to use a dummy plot out of the boundaries, plotting the relevant data and calling the legend just at the end. It works but it is prone to errors. I wanted to update the legend and select just a few of the plots.
I tried to use the leg_handle.String, but then it comes two problems:
It still plot 5 handles instead of 3.
It does not have the proper line style & color.
Any ideas?
Bellow follow the code (with dummy plot commented) and the pictures of the current version giving the error and what i want to look.
clear
figure()
hold on
%using
%dummy plot
% leg_text={'a','b','c'};
% plot(100,100,'-r')
% plot(100,100,'-b')
% plot(100,100,'-k')
for ii=1:20,
plot(1:11,linspace(0,ii,11),'-r')
end
for ii=30:50,
plot(1:11,linspace(0,ii,11),'-b')
end
for ii=70:80,
plot(1:11,linspace(ii,25,11),'-k')
end
Yaxl=[-1 80];
Xaxl=[1 11];
set(gca, 'Visible','on', ...
'Box','on', ...
'Layer','top',...
'Xlim',Xaxl, ...
'Ylim',Yaxl);
%using
% legend(leg_text)
%want to use
leg_hand=legend(gca,'show');
leg_hand.String=leg_hand.String([1 21 42]);
%extra comand will give the things that i wanted above
% leg_hand.String=leg_hand.String([1 2 3]);
What it gives:
What I expect to have:
I have tried this method using [a,b,c,d]=legend, but this give only the a handle that i already using.
This little workaround should do the job:
clear();
figure();
hold on;
h = gobjects(3,1);
for ii = 1:20
h(1) = plot(1:11,linspace(0,ii,11),'-r');
end
for ii = 30:50
h(2) = plot(1:11,linspace(0,ii,11),'-b');
end
for ii = 70:80
h(3) = plot(1:11,linspace(ii,25,11),'-k');
end
set(gca,'Box','on','Layer','top','Visible','on','Xlim',[1 11],'Ylim',[-1 80]);
legend(h,'A','B','C');
hold off;
Actually, what I did is very simple. I created an array of graphical objects of size 3 (one for each iteration) using the gobjects function. Then, inside each iteration, I assigned the last plotted line to its respective array placeholder. Finally, I created the legend using the three graphical objects I previously stored.
Alternatively:
clear();
figure();
hold on;
h1 = gobjects(20,1);
for ii = 1:20
h1(ii) = plot(1:11,linspace(0,ii,11),'-r');
end
h2 = gobjects(21,1);
for ii = 30:50
h2(ii-29) = plot(1:11,linspace(0,ii,11),'-b');
end
h3 = gobjects(11,1);
for ii = 70:80
h3(ii-69) = plot(1:11,linspace(ii,25,11),'-k');
end
set(gca,'Box','on','Layer','top','Visible','on','Xlim',[1 11],'Ylim',[-1 80]);
legend([h1(1) h2(1) h3(1)],'A','B','C');
hold off;
You create an array of graphical objects for storing the plot handlers produced by every iteration. Then you create the legend using the first (basically, any) item of each array of graphical objects.

Matlab update plot with multiple data lines/curves

I want to update a plot with multiple data lines/curves as fast as possible. I have seen some method for updating the plot like using:
h = plot(x,y);
set(h,'YDataSource','y')
set(h,'XDataSource','x')
refreshdata(h,'caller');
or
set(h,'XData',x,'YData',y);
For a single curve it works great, however I want to update not only one but multiple data curves. How can I do this?
If you create multiple plot objects with a single plot command, the handle returned by plot is actually an array of plot objects (one for each plot).
plots = plot(rand(2));
size(plots)
1 2
Because of this, you cannot simply assign another [2x2] matrix to the XData.
set(plots, 'XData', rand(2))
You could pass a cell array of new XData to the plots via the following syntax. This is only really convenient if you already have your new values in a cell array.
set(plots, {'XData'}, {rand(1,2); rand(1,2)})
The other options is to update each plot object individually with the new values. As far as doing this quickly, there really isn't much of a performance hit by not setting them all at once, because they will not actually be rendered until MATLAB is idle or you explicitly call drawnow.
X = rand(2);
Y = rand(2);
for k = 1:numel(plots)
set(plots(k), 'XData', X(k,:), 'YData', Y(k,:))
end
% Force the rendering *after* you update all data
drawnow
If you really want to use the XDataSource and YDataSource method that you have shown, you can actually do this, but you would need to specify a unique data source for each plot object.
% Do this when you create the plots
for k = 1:numel(plots)
set(plots(k), 'XDataSource', sprintf('X(%d,:)', k), ...
'YDataSource', sprintf('Y(%d,:)', k))
end
% Now update the plot data
X = rand(2);
Y = rand(2);
refreshdata(plots)
You can use drawnow:
%Creation of the vectors
x = 1:100;
y = rand(1,100);
%1st plot
h = plot(x,y);
pause(2);
%update y
y = rand(1,100);
set(h,'YData',y)
%update the plot.
drawnow

Plotting on MATLAB

I am trying to plot a cobweb diagram using the following code:
function cobweb(f,a,b,x0,x1,N)
x(1)=0.2; % plot orbit starting at x0
for i=1:100
x(i+1)= 3*x(i)*(1-x(i));
plot([x(i),x(i)],[x(i),x(i+1)]);
hold on
plot([x(i),x(i+1)],[x(i+1),x(i+1)]);
hold on
end
hold on
r = 3;
x = 0:0.01:1; %// set some x
f = (r.*x.*(1-x));
hold on
plot(x,f,'k')
hold on
plot([x(1), 0], [x(1), 3*x(1)*(1-x(1))])
I want the first line to be drawn up from the x-axis, however it's currently starting from (x(1),x(1)), where x(1) is the initial point.
I understand that this is because of my loop so I tried adding an extra plot for the initial line by plot([x(1), 0], [x(1), 3*x(1)*(1-x(1))]), however I still get the same result.
The function I am plotting is f = 3x(1-x).
You probably want to move that line:
plot([x(1), 0], [x(1), 3*x(1)*(1-x(1))])
before the for loop and add a hold on after it before you enter the loop. You can then remove the other hold on inside the for loop and further down the code, as they are redundant.

Logarithmic Y scale, multiple value plot

I'd like to plot mutiple values onto the same graph with a logarithmic Y scale. The following code plots the values onto a linear scale graph and works, however trying to change 'plot' with 'semilogy' outputs a blank graph.
hold on;
for i = 1:10
[val1(i), val2, val3, val4] = myFunct(i, fileName);
end;
plot(val1);
hold off;
What do I need to change to create a Y scale that is logarithmic?
Edited code:
hold on;
for i = 1:10
[val1(i), val2, val3, val4] = myFunct(i, fileName);
end;
val1(1) = 0.000001; %index 1 is always zero, index 2 may or may not be zero
val1(2) = 0.000001;
semilogy(val1);
hold off;
Output of the above code:
Try this:
% Calculate
for i = 1:10
[val1(i), val2, val3, val4] = myFunct(i, fileName);
end
% Plot
figure;
plot(val1+eps);
set(gca, 'YScale','log');
The hold on command prevents the figure from being updated from the regular plot you did before to semilogy. To solve this you should close your figure and run the code again.
Note that there is no reason to use hold commands if you only have one plotting command. The purpose of hold is to enable several plotting commands to be overlayed in the same figure.