How to plot contour from points coordinate in matlab - matlab

I have an image and four points (x1,y1),(x2,y2),(x3,y3),(x4,y4). They are pixel coordinates of image. I want to generate one contour from points around image using Matlab. I try to make it but it does not accuracy. Which does implement better?
X=[x1 x2 x3 x4];
Y=[y1 y2 y3 y4];
BW1 = roipoly(I,X,Y); % I is an image
figure,
imagesc(uint8(I),[0 255]),colormap(gray);axis equal;axis off;
hold on,[c1,h1] = contour(BW1,[0 0],'r','linewidth',1.4); hold off

Maybe this is what you are trying to do (I am guessing here…):
X=[x1 x2 x3 x4];
Y=[y1 y2 y3 y4];
BW1 = roipoly(I,X,Y); % I is an image
figure
imagesc(uint8(I),[0 255])
colormap(gray);
axis equal;axis off;
hold on;
plot([X X(1)], [Y Y(1)], 'r', 'linewidth', 1.4); % <<<<< using plot instead of contour
or perhaps you need
[c1,h1] = contour(BW1,[0 0] + 0.5,'r','linewidth',1.4); hold off
This second case uses the fact that a contour of 0.5 will sit right on the boundary between 0 and 1, as needed. But I suspect that your BW1 image will overwrite the original image… sorry can't test right now and you left a lot to be guessed.

Related

Matlab: Plots and Subplots - How do I make Matlab zoom into all the subplots simultaneously?

I have multiple sub-plots. When I zoom into the first subplot to view a certain data set Matlab does not zoom the rest of the subplots. How do I make Matlab zoom into all subplots simultaneously?
subplot(2,1,1)
hold on
plot(Tim1.in,IA1.raw32SPC,'b-')
hold off
subplot(2,1,2)
hold on
plot(Tim1.in,IA1.cos,'r-*')
hold off
Since the x variable is the same, It makes sense to just link the x axes in this case. I added some code to create the x and y variables so that anyone can run the code below. You were also using hold unnecessarily.
x = 1:10;
y1 = 3*x;
y2 = x.^2;
ax(1) = subplot(2,1,1);
plot(x, y1, 'b-')
ax(2) = subplot(2,1,2);
plot(x, y2, 'r-*')
linkaxes(ax, 'x')

Plot Multiple Lines with Different Scaling Matlab

I'm trying to plot a few waveforms onto the same plot in Matlab; brand new to it. I've tried plotting them all together with plot() but it doesn't scale them all appropriately. How would I go about scaling them? I read something online that uses hold on, but I'm getting the same issue I believe. What's the simple solution here?
t1 = 0:0.1:1000;
y1 = t1.^5-5*t1.^3+4*t1;
plot(t1, y1)
hold on
t2 = 0:0.0001:0.01;
y2 = -8*exp(-1000*t2) + 3;
plot(t2, y2)
hold on
t3 = 0:0.0001:0.6;
y3 = exp(-10*t3).*cos(100*t3);
plot(t3, y3)
hold on
%plot(t1, y1, t2, y2, t3, y3)
Matlab is doing what you ask it to do: plotting everything to the same axis system (by the way, you only need to use hold on once, it is active until you change the axis or command hold off)
You have three options
define the limits of the axis explicitly, either separately using xlim([xmin xmax]) and ylim([ymin ymax]) or jointly using axis([xmin xmax ymin ymax]) (in your case e.g. axis([0 0.6 0 3.3])
you may want to use separate axis in one plot, see yyaxis yyaxis left / yyaxis right to activate the axes. Note that this only provides two different axes scales
use subplots! (as Cris Luengo already stated)
That would be in your case
% 1st subplot
t1 = 0:0.1:1000;
y1 = t1.^5-5*t1.^3+4*t1;
ax(1) = subplot(3,1,1);
plot(t1, y1)
% 2nd subplot
t2 = 0:0.0001:0.01;
y2 = -8*exp(-1000*t2) + 3;
ax(2) = subplot(3,1,2);
plot(t2, y2)
% 3rd subplot
t3 = 0:0.0001:0.6;
y3 = exp(-10*t3).*cos(100*t3);
ax(3) = subplot(3,1,3);
plot(t3, y3)
% link all axes (but only x direction)
linkaxes(ax,'x')
% your axes limits are so large that you probably don't want to link them

plotting a bullet-nose curves

I would like to plot this function of Two Variables you can find it here
$$z^2=t(t-i) \Longleftrightarrow x^2+y^2=4x^2y^2 \Longleftrightarrow y=\dfrac{\pm x}{\sqrt{4x^2-1}} \mbox{ with } |x|>\frac{1}{2}$$
would someone show me step by step how to plot this in matlab
is there any script or toolbox in http://www.mathworks.com/matlabcentral/fileexchange
which make plot of that kind of curves quickly
this is by geogebra
This is by wolframe
You can use symbolic variables with ezplot.
syms x y % makes symbolic variables
h1 = ezplot('-4*x^2*y^2+x^2+y^2'); % plots the equation
axis equal
set(h1, 'Color', 'k');
Or you can define a function,
f = #(x,y) -4.*x.^2.*y.^2+x.^2+y.^2;
h1 = ezplot(f);
set(h1, 'Color', 'k');
It won't be easy to have the axis in the middle, I hope it's not necessary to have that.
Edit
You can download oaxes here
syms x y
h1 = ezplot('-4*x^2*y^2+x^2+y^2');
axis equal
set(h1, 'Color', 'm');
oaxes('TickLength',[3 3],'Arrow','off','AxisLabelLocation','side',...
'LineWidth',1)
Edit
For 3D plot try this,
% First line provides a grid of X and Y varying over -5 to 5 with .5 as step-size
[X,Y] = meshgrid(-5:.5:5);
% instead of "=0", Z takes the values of the equation
Z = -4 .* X.^2 .* Y.^2 + X.^2 + Y.^2;
surf(X,Y,Z) % makes a 3D plot of X,Y,Z
You can also try contourf(X,Y,Z) for 2D plot.

plot in all the subplots at the same time

I have a figure with 2 subplots.
I would like to know if it's possible (and how) to draw the same plots in all the subplots at the same time.
For example in the following plot I'd like to plot (x,y) simultaneously and then proceed separately.
fig1 = figure
subplot(2,1,1)
plot(x,y)
hold on
subplot(2,1,2)
plot(x,y)
hold on
subplot(2,1,1)
plot(x,z)
subplot(2,1,2)
plot(x,k)
You can do it with set using cell arrays as follows. See the documentation for details.
subplot(2,1,1);
h1 = plot(x,y); %// get a handle to the plot
subplot(2,1,2)
h2 = plot(x,y); %// get a handle to the plot
set([h1; h2], {'xdata'}, {x1; x2}, {'ydata'}, {y1; y2})
%// new values: x1 x2 y1 y2
If you're asking because you want to plot the same plot behind say 16 subplots, then you could do it in a loop:
for k= 1:16
s(k) = subplot(4,4,k);
plot(x,y);
hold on;
end
If you just want it for 2 subplots then there is nothing wrong with your current code

Multiple Data Sets on the Same Scatter

I know this question may sound a bit easy, but I couldn't find what I wanted in the documentation. Basically, I would like to know if a function exists in matlab which allows me to plot data sets y1, y2, ..., yn against the same x-axis in the same scatter diagram.
Any suggestions?
Thanks
You can just use scatter + hold on. For example,
x = rand(1,10);
y1 = rand(1,10);
y2 = rand(1,10);
y3 = rand(1,10);
figure; grid on;
hold on;
scatter(x, y1);
scatter(x, y2);
scatter(x, y3);
Gives: