Logarithmic Y scale, multiple value plot - matlab

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.

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.

bode plot discrepancy

I am plotting the following
Cu4 = tf([1 2], [1 2 6]);
[magCu4 phaseCu4 wout] = bode(Cu4,logspace(-2,7,300));
magCu4 = squeeze(magCu4);
phaseCu4 = squeeze(phaseCu4);
semilogx(wout,20*log10(magCu4)),grid;
hold on
bode(Cu4,'r')
I would expect that the semilogx plot would return an identical plot as 'bode'. however, this doesn't seem to be the case. Does anyone know what is going wrong here?
The difference is that you do not specify a frequency vector in your second call to bode so MATLAB chooses a default vector (in your code it had length of 46).
Instead you could try:
bode(Cu4,'r',logspace(-2,7,300))
Compare the plots made by the following code
[magCu4 phaseCu4 wout] = bode(Cu4,logspace(-2,7,300));
magCu4 = squeeze(magCu4);
figure(1);
semilogx(wout,20*log10(magCu4))
hold on;
bode(Cu4,'r')
hold off;
figure(2);
semilogx(wout,20*log10(magCu4))
hold on;
bode(Cu4,'r',logspace(-2,7,300))
hold off;

How to plot two figures in MATLAB

I am implementing a clustering algorithm for n data points and I want to plot n data points in a figure before clustering and in another figure after clustering meaning that there should be two figures in same file with same data points.
My code is like:
X = 500*rand([n,2]);
plot(X(:,1), X(:,2), 'r.') 1
%Some coding section here
After:
symbs = {'r+','g.','bv','m*','ko'};
hold on
for i = 1: length(I)
plot(X(C==i,1), X(C==i,2), symbs{i}) 2
end
I just want to plot (1) in one figure and (2) in another.
Try subplot:
figure;
subplot(1,2,1)
plot(firstdata)
subplot(1,2,2)
plot(seconddata)
This will create two axes areas within the same figure window... from your description, this is my best guess as to what you want.
Edit: From the comments below, here is what you are doing
n=50;
X = 500*rand([n,2]);
subplot(1,2,1); #% <---- add 'subplot' here
plot(X(:,1),X(:,2),'r.')
symbs= {'r+','g.','bv','m*','ko'};
subplot(1,2,2); #% <---- add 'subplot' here (with different arguments)
hold on
for i = 1: length(I)
plot(X(C==i,1),X(C==i,2),symbs{i})
end
If all you want is a second figure window, instead of doing subplot you can simply say figure in the place where I put the second call to subplot and a new figure window will be created.
figure; #% <--- creates a figure window
n=50;
X = 500*rand([n,2]);
plot(X(:,1),X(:,2),'r.') #% <--- goes in first window
symbs= {'r+','g.','bv','m*','ko'};
figure; #% <---- creates another figure window
hold on
for i = 1: length(I)
plot(X(C==i,1),X(C==i,2),symbs{i}) #% <--- goes in second window
end
You just need to add figure before each plot to get two plots in two separated figures

Subplots of multidimensional arrays in 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