Adding a curve to Matlab Figure - matlab

I have written a program that takes a lot of data and produces graphs.It would be really convenient and save me a lot of time if I could take curves on an existing figure and add their values together to make a single curve. For a simple example lets say I have the following code,
x = [0 1 2 3 4 5];
y = [0 1 2 3 4 5];
z = [4 6 2 8 7 9];
figure
plot(x,y,x,z)
This code will produce a figure with two curves. Without modifying the code or re-running the program, and only working with the figure options I would like to add the curve y + z to the plot. Is this possible? Thanks.
The reason I don't want to add the functionality is the plot code is buried within 8 loops
that calls data from a 4D cell array of file name strings.

If you have the x, y and z variable used in the plot you can just add new lines to the plot with
hold on
plot(x,y+z)
hold off
If you don't have them directly (they were generated in a function, for example, you can always get them from figure with XData, YData properties of line objects.
hline = findobj(gca,'type','line');
x = get(hline,'XData');
y = get(hline,'YData');
X = x{1}; % let's assume that all lines have the same x values.
Y = sum(cell2mat(y));
hold on
plot(X,Y)
hold off

Related

Exchange subplots in figure

I create a figure containing 4 subplots like:
y = [2 2 3; 2 5 6; 2 8 9; 2 11 12];
fig = figure
for i = 1 : 4
h(i) = subplot(1,4,i)
bar(y)
end
Now I would like to exchange the order of the subplots. For example the first subplot should go in the last column (4th) and the 2nd subplot should exchange with the 3rd.
Is it doable without generating again the figure?
Perhaps you could change their 'position' of the axes h. For instance:
% get position
pos = get(h,'position');
% change position, 1st <-> 4th, 2nd <->3rd.
set(h(1),'position',pos{4});
set(h(4),'position',pos{1});
set(h(2),'position',pos{3});
set(h(3),'position',pos{2});
There is a possibility which kind of regenerates the figure. You can go to the plot tools, right click the figure and select "Show code". This will open a new file with a function that recreates the figure. There you can change your subplot-position and call the function in order to obtain the figure with switched subplots.
While this recreates the figure, you do not have to re-evaluate the function or script that originally created the figure.
PS: I'm not sure how new this feature is.
In addition to swapping the Position property of the axes objects, you could also swap the Parent property of the bar objects. This has the advantage of leaving your axes positioning intact.
A simple example:
function testcode
y1 = [2 2 3; 2 5 6; 2 8 9; 2 11 12];
y2 = -y1;
h(1) = subplot(1, 2, 1);
b(1,:) = bar(y1);
h(2) = subplot(1, 2, 2);
b(2,:) = bar(y2);
swapbar(h, b, [1,2]);
end
function swapbar(axesobjects, barobjects, axesswap)
set(barobjects(axesswap(1), :), 'Parent', axesobjects(axesswap(2)));
set(barobjects(axesswap(2), :), 'Parent', axesobjects(axesswap(1)));
end
Here I've created a small helper function swapbar to swap two axes. This isn't at all a robust implementation but it works well to illustrate the concept.
Old:
New:

drow cumulative distribution function in matlab

I have two vectors of the same size. The first one can have any different numbers with any order, the second one is decreasing (but can have the same elements) and consists of only positive integers. For example:
a = [7 8 13 6];
b = [5 2 2 1];
I would like to plot them in the following way: on the x axis I have points from a vector and on the y axis I have the sum of elements from vector b before this points divided by the sum(b). Therefore I will have points:
(7; 0.5) - 0.5 = 5/(5+2+2+1)
(8; 0.7) - 0.7 = (5+2)/(5+2+2+1)
(13; 0.9) ...
(6; 1) ...
I assume that this explanation might not help, so I included the image
Because this looks to me as a cumulative distribution function, I tried to find luck with cdfplot but with no success.
I have another option is to draw the image by plotting each line segment separately, but I hope that there is a better way of doing this.
I find the values on the x axis a little confusing. Leaving that aside for the moment, I think this does what you want:
b = [5 2 2 1];
stairs(cumsum(b)/sum(b));
set(gca,'Ylim',[0 1])
And if you really need those values on the x axis, simply rename the ticks of that axis:
a = [7 8 13 6];
set(gca,'xtick',1:length(b),'xticklabel',a)
Also grid on will add grid to the plot

Scatter plot grouped by row for a 31 x 3 2d array

I have a 2d array and I want to scatter plot the points grouped by row so that each row has a different symbol. This is my code so far, all the points are the same symbol so I can't tell which points are part of which row.
a = zeros (31,3);
for k = 0:30
y = 5*k
dent = [1 10 10 y]
a(k+1, [1 2 3]) = roots(dent)
end
t = 1:3
gscatter(real(a(:,t)),imag(a(:,t)));
You do not need a loop, you can exploit the gscatter options:
a = zeros (31,3);
for k = 0:30
y = 5*k;
dent = [1 10 10 y];
a(k+1, [1 2 3]) = roots(dent);
end
group = ones(size(a));
group(:,1) = group(:,1).*0;
group(:,3) = group(:,3).*2;
gscatter(real(a(:)),imag(a(:)),group(:),'brg','xo+');
You need an additional vector, group, which contains information on which points in your data-set belong to a specific group. This variable is very versatile, see it's documentation.
In your case, I suggest setting up a matrix that is 0 in the first column, 1 in the second and 2 in the third.
In the gscatter function call, reshape all your matrices into vectors using (:) (because gscatter only works with vectors.
The other two strings passed to gscatter:
'brg'
'xo+'
determine the color and shape of the symbols, respectively. Your plot then looks like this:
EDIT
For those users without access to the gscatter function, this is how it can be done using scatter:
s = 40;
hold on
COLORS='brg';
SYMBOLS='xo+';
for t=1:size(a,2)
scatter(real(a(:,t)),imag(a(:,t)),s,SYMBOLS(t),'MarkerEdgeColor',COLORS(t))
end
hold off
A few things to take note of:
to be used in this way, scatter needs a symbol-size, which was set to s = 40 in this example.
the symbols are stored in a string variable so that the can be called in the loop.
the same is true for the edge colors (face colors could also be specified, check the scatter documentation
when called in a loop, use hold to plot into the same figure (roughly speaking)
This is the output from the standard scatter plot:

Smoothing out of rough plots

I want to draw some plots in Matlab.
Details: For class 1, p(x|c1) is uniform for x between [2, 4] with the parameters a = 1 and b = 4. For class 2, p(x|c2) is exponential with parameter lambda = 1. Besides p(c1) = p(c2) = 0.5 I would like to draw a sketch of the two class densities multiplied by P(c1) and P(c2) respectively, as
a function of x, clearly showing the optimal decision boundary (or boundaries).
I have the solution for this problem, this is what the writer did (and I want to get), but there's no Matlab code, so I want to do it all by myself.
And this is what I drew.
And this is the MATLAB code I wrote.
x=0:1:8;
pc1 = 0.5;
px_given_c1 = exppdf(x,1);
px_given_c2 = unifpdf(x,2,4);
figure;
plot(x,px_given_c1,'g','linewidth',3);
hold on;
plot(x,px_given_c2,'r','linewidth',3);
axis([0 8 0 0.5]);
legend('P(x|c_1)','P(x|c_2)');
figure;
plot(x,px_given_c1.*pc1,'g','linewidth',3);
hold on;
plot(x,px_given_c2.*(1-pc1),'r','linewidth',3);
axis([0 8 0 0.5]);
legend('P(x|c_1)P(c_1)','P(x|c_2)P(c_2)');
As you can see, they are almost smiliar, but I am having problem with this uniform distribution, which is drawn in red. How can I change it?
You should probably change x=0:1:8; to something like x=0:1e-3:8; or even x=linspace(0,8,1000); to have finer plotting. This increases number of points in vectors (and therefore line segments) Matlab will use to plot.
Explanation: Matlab works with line segments when it does plotting!
By writing x=0:1:8; you create vector [0 1 2 3 4 5 6 7 8] that is of length 9, and by applying exppdf and unifpdf respectively you create two vectors of the same length derived from original vector. So basically you get vectors [exppdf(0) exppdf(1) ... exppdf(8)] and [unifpdf(0) unifpdf(1) ... unifpdf(8)].
When you issue plot command afterwards Matlab plots only line segments (8 of them in this case because there are 9 points):
from (x(1), px_given_c1(1)) to (x(2), px_given_c1(2)),
...
from (x(8), px_given_c1(8)) to (x(9), px_given_c1(9)).

Removing the line between two specific data points in Matlab

I am going to draw a graph in Matlab. The graph is quite simple and I am using the plot function.
Suppose the data that I want to plot is (0:1:10). I also put markers on my graph. Then, we have a line that has markers on coordinates (0,0),(1,1),(2,2),... etc.
Now, I want to remove the line between (2,2) and (3,3) without deleting the whole line. That is to say, my purpose is to get rid of a particular segment of the line without loosing the entire line or any marker points.
How can I do that?
Removing the section of line after you have plotted it is difficult. You can see that the line is made up of one MATLAB object by the following code:
x = 1:10;
y = 1:10;
H = plot(x, y, '-o');
get(H, 'children')
ans =
Empty matrix: 0-by-1
We can see that the line has no children, so there are no 'subparts' that we can remove. However, there are some cheeky tricks we can use to try to achieve the same effect.
Plot two lines separately
...using hold on. See Victor Hugo's answer. This is the proper way of achieving our goal.
Plot two separate lines in one
MATLAB doesn'y plot points with a NaN value. By modifying the input vectors, you can make MATLAB skip a point to give the effect of a broken line:
x = [0 1 2 2 3 4 5 6 7 8 9];
y = [0 1 2 nan 3 4 5 6 7 8 9];
plot(x, y, '-o');
This is equivalent to plotting a line from [0, 0] to [2, 2], skipping the next point, then starting again at [3, 3] and continuing to [9, 9].
'Erase' part of the line
This is the nastiest way of doing it, but is a cheap hack that could work if you can't be bothered with changing your input arrays. First plot the line:
x = 1:10; y = 1:10;
plot(x, y, '-o');
Now plot a white line over the part you wish to erase:
hold on
plot([2 3], [2 3], 'w');
As you can see, the result doesn't quite look right, and will respond badly if you try to do other things to the graph. In short, I wouldn't recommend this method but it might come in useful in desperate times!
Try the following:
y = [0.2751 0.2494 0.1480 0.2419 0.2385 0.1295 0.2346 0.1661 0.1111];
x = 1:numel(y);
plot(x(1:4), y(1:4), '-x')
hold
plot(x(5:end), y(5:end), '-x')