Multiple graphs in the same window - matlab

i have a function that gets few parameters and plots a graph in Matlab
i want to use the function to plot few graphs with different parameters in one window.
i tried "subplot" but it doesn't do it.
is there a way doing it?
thanks

Use the command hold on and in the end hold off
Further information: http://www.mathworks.ch/ch/help/matlab/ref/hold.html

I would like to add one more thing in C. Colden suggestion, Provide the same x values for all plot as shown below:
x = 0:0.01:pi ;
y1 = sind(x) ;
y2= codd(x) ;
plot (x,y1)
hold on
plot(x,y2)
if you want to provide x1 and x2 for x values, then you will encounter with an error.

Related

Matlab: Plotting on same figure using 2 different functions

I have written a function of the form
function myplot(x,y)
plot(x,y)
end
This function creates a plot for given values of x and y. The actual function is more complex, but it does not serve the purpose of the question to include its content here. The main point follows.
I have tried to run the following script:
x = [1:0.01:10]
y = [1:0.01:10]
figure
plot(sin([1:0.01:10]))
hold on
myplot(x,y)
The intent here is to plot 2 sets of data on the same graph. The first set of data is generated by Matlab's native plot command while the second set of data is generated by the user custom myplot function (in this case what should be a straight line). The script above wont do it....
How to get Matlab to include both sets of data on the same plot?
your script plots them both but with different x values. if you don't specify x input in plot it uses 1:length(y), while your myplot function does specify x values (which in your case are 10-fold smaller).
just do: plot(x,sin([1:0.01:10])) instead of plot(sin([1:0.01:10]))
You could save the current axes (on which you create the first plot) in a variable and pass it as an argument to your function to be sure it gets plot on the same axes no matter what happens elsewhere in your code.
So your main code could look like this:
x = [1:0.01:10];
y = [1:0.01:10];
figure
plot(sin([1:0.01:10]))
hold on
%// Save axes in variable
CurrentAxes = gca;
%// Pass it as argument to function
myplot(x,y,CurrentAxes)
and the function:
function myplot(x,y,hAxes)
plot(hAxes,x,y);
end

Create cos graphs using matrix-based method on MATLAB?

I've plotted cos(x), cos(2x) and cos(3x) on a graph using following:
x=linspace(0,4*pi,50); y=cos(x)
plot(x,y)
y2 = cos(2*x)
hold on, plot(x,y2)
y3 = cos(3*x)
hold on, plot(x,y3)
grid on
xlabel (‘x’), ylabel(‘y’)
legend (‘y=cos(x)’, ‘y=cos(2x)’,’y=cos(3x)’)
How can I do it a different way using a matrices? If I create a 3 columned matrix representing cos(x), cos(2x) and cos(3x) by using: Y=[sin(x) sin(2*x) sin(3*x)]. What would I do after this? I typed in plot(x,Y) but says Error using plot. Vectors must be the same length. Perhaps an obvious thing but only recently started using MATLAB. Thanks in advance.
You're concatenating horizontally using the spaces (i.e., creating one long vector that is three times the length of x). If you concatenate vertically using ; to create a 3x50 matrix, everything will be fine:
x = linspace(0,4*pi,50);
Y = [cos(x);cos(2*x);cos(3*x)];
plot(x,Y);

Issue when creating a code for a plot on Matlab

I am trying to create a plot using the below code, the plot it produces incorporates values of the x-axis from 0.5-1, however I need the plot to not incorporate this part on the x-axis, from values 0.5-1. I assumed that lines 9 & 10 of the code would exclude this part of the plot, however this is not the case.
Does anyone know what I would write for this part of the plot to be excluded. Thank you!
x = [0:0.01:1];
y = [0:0.01:1];
z = size(101,101);
for j = 1:101;
for k = 1:101;
z(j,k)=((x(j))./(0.5-y(k)));
if z(j,k)>1
z(j,k)=1;
elseif z(j,k)<0
z(j,k)=1;
end
end
end
surf(x,y,z)
xlim([0 .5]) sets the range to be displayed on the x axis. Use it after your plot command (surf).
x=[0:0.005:0.5];
This would exclude the data you do not want to see in the plot, but would give you better resolution for your answer.
MWc answer would work as well, but would just exclude the values from 0.5 on.
So it depends on exactly what you wanted to see given your overall problem.

plotting symfun and a self created function on same graph matlab

i am trying to plot two function in matlab, the first one is of kinf symfun:
p = symfun(0, [m]);
p(m) = p(m)+Ck(k-3)*exp(m*(k-3)*complex(0, 2*pi/25));
here Ck is another symfun and k is a variable i pre-defined.
i want to plot it in the same graph with a function i created using the function mode:
function [x1] = xt_otot_q3( t)...
i cant make the xt_otot_q3 function a symfun because it involves if statements.
- i tried to create 2 vectors sampling the two functions and plotting them together with the plot function but for some reason the 'p' function vectors gets preatty grotesque giving me wierd output...
- i tried plotting them both using ezplot function but for some reason the sampled vector i got form xt_otot_q3 shows only as a straight line at 0.
any ideas how i should plot them together? to plot the xt_otot_q3 function i must create a vector if i try to plot it directly using ezplot it gives me the following eror:
>> ezplot(xt_otot_q3, [-10 10])
Error using xt_otot_q3 (line 2)
Not enough input arguments.
thanks in advance.
If I am understanding it properly, you have two functions p, and xt_otot_q3. You want to plot them together.
syms t;
func1 = xt_otot_q3(t);
ezplot(func1, [-10 10]);
# retain current graph, for new graph
hold on;
# symbolic function p
ezplot(p, [-10 10]);
I hope it helps.

MATLAB: Dynamic heatmap

I have 3D matrix of dimensions D x D x N. I want to create a dynamic heatmap to show how it's varying over N. Here's the MATLAB code I used to achieve this.
for n=1:N
heatmap(dynamicCov(:,:,n));
pause(0.5);
end
The issue with this code is that for each n, it opens a new figure window. I want it to be updated in the same Heatmap window. Is it possible to do that? Is there any other way to achieve this?
Thanks.
You need to use the undocumented 2nd input to HeatMap that indicates whether a plot should be created or not, and a few other Handle Graphics tricks to get the handle to the figure that is created. Something like
data = rand(20,20,10); % create test data
hmo = HeatMap(data(:,:,1),false); % create but do not plot
plot(hmo); % do the plot
allHFig = findall(0,'Type','figure'); % get handle to all open figures
hFig = allHFig(1); % we want the most recently created figure
for idx = 2:size(data,3)
hmo = HeatMap(data(:,:,idx),false); % create heatmap but do not plot
plot(hmo,hFig); % plot to our existing figure
pause(0.5);
end
I found a better and a lot simpler way of doing this. It uses built in imagesc() function instead of HeatMap() function from Bioinformatics toolbox. The code is as follows:
dynamicCov = rand(20,20,10); % create test data
N = size(dynamicCov,3);
for n=1:N
imagesc(dynamicCov(:,:,n));
colormap('copper');
colorbar;
pause(0.5);
end
Reference: http://buli.waw.pl/matlab-heatmap-colormap/