Making MatLab show different plots on two windows - matlab

So I have written a program in which we want MatLab to plot given data on a scatter plot and bar graph.
I can not really write the complete code here since its pretty long. So I will just describe the problem:
MatLab plots both graph (scatter and bar graph) but since the command for scatter plot comes after the bar graph, therefore Matlab deletes the bar graph and instead plots and gives out the scatter plot.
IS there a command which would allow Matlab to show both plots on two different windows.

Simply call figure; before plotting a second time. It might look something like this:
plot(...); % plot bar graph
figure;
plot(...); % plot scatter plot

Related

What exactly does Matlab do to plot a spectrogram?

I'm trying to plot a spectrogram of a vibration signal using Matlab, but I'm not happy with the way that the spectrogram-function plots the signal (I would like to customize the axes and use a mapped vector instead of time). Right now I'm trying to plot the thing myself using pcolor:
[M_s, M_w, M_t] = spectrogram(M_i, 2^16, ceil(2^16*0.95), [0:0.2:15000], Sample_rate);
figure;
[M_t, M_w] = meshgrid(M_t, M_w);
h = pcolor(M_t, M_w, mag2db(abs(M_s)));
set(h, 'LineStyle', 'none);
However, doing this, my plotted figure is extremely slow and I can't really work with that, because it takes ages to zoom in or do anything with it. When I just use spectrogram to plot the figure, it's super fast. So my question is, what exactly does spectrogram do, to plot the figure?

Legend each curve in a single figure from a matlab plot

I want to plot four curves in a single figure from matlab, so I am using hold on. Furthermore, I want to create an legend to each curve, so I wrote the code:
clear all
x=linspace(0,10,100);
x2=linspace(-5,15,100);
x3=linspace(-10,20,100);
x4=linspace(35,40,100);
figure(1)
plot(x,x2)
legend('x2')
hold on
plot(x,x3)
legend('x3')
hold on
plot(x,x4)
legend('x4')
hold on
plot(x,x)
legend('x')
hold off
But the result is that all my curves are in the same color, and just the last legend "x" has appeared in the figure (see it below).
How can I set one legend to each curve? All curves must have different colors.
This depends a bit on your matlab version. In older versions (and in octave), plots added through the use of hold on get the same color. In R2015b (i don't know when this was introduced), the individual plots get different colors, but still only one legend is displayed.
To get multiple colors and multiple legend entries, you can specify all data to be plotted in one call, the same for the legends:
plot(x, x, x, x2, x, x3, x, x4);
or
plot(x, [x', x2', x3', x4']);
For the legends, approach the same way:
legend('x', 'x2', 'x3', 'x4');
If you want to build up a legend without knowing the number of entries before hand, you would want to search for "dynamic legends". See for example here:
http://undocumentedmatlab.com/blog/legend-semi-documented-feature

how to write some text in the plot in matlab

I wrote the following code in matlab for plotting a graph. I have three curves in the same plot. I want to mention the particular class to each curve to which it belongs.How can I do it using same code that i wrote. Code is:
plot(mm,q1(1,:),'b')
hold on
plot(mm,q1(2,:),'g')
plot(mm,q1(3,:),'r')
plot(mm,q1(4,:),'m')
xlabel('time(yr)')
ylabel('probability')
hold off
I want to write 'M1 to M2'in plot corresponding to graph for plot(mm,q1(1,:),'b'). Similarily for other plots 'M1 to M2' , 'M1 to M3' ,'M1 to M4' respectively.
You can use legend as follows:
legend('M1 to M2','M1 to M3','M1 to M4');
This allows you to label curves.

matlab fit a HeatMap into a subplot

I tried to fit a HeatMap as a subplot of a plot. However it seems they are not compatible. Every time using a HeatMap function, it seems it will always open a HeatMap 'canvas'. If save the result of HeatMap as a object and use plot or view to put it into a figure, it always open a new figure window rather than plot on the existing one, even with the hold on; command. Is there a way to make the HeatMap as one of the subplots?
A heatmap example code:
y = [1,2,3,4;5,6,7,8;4,3,2,1;8,7,6,5];
obj = HeatMap(y,'Symmetric','false','colormap','jet'); %this will generate a HeatMap canvas
plot(HeatMap); %this will display or render the heatmap object into a figure window
It seems I should use imagesc rather than HeatMap function as imagesc is more compatible. By using imagesc to plot the matrix, I can easily set the heatmap as a subplot.

Plotting multiple histograms in one 3d

I've got multiple vectors of which I would like to plot seperate histograms in the same figure. However, if I plot all histograms in the same 2D plot, the results become hard to interpret/distinguigh. Therefore I would like to plot them (detached) in 3D.
All I have right now is the following:
hist(A)
hold on
hist(B)
hist(C)
holf off
checkout bar3 for 3D bar plots.