- You will see updates in your activity feed.
- You may receive emails, depending on your notification preferences.

24 views (last 30 days)

Show older comments

Shri Harsha Bharadwaj
on 16 Feb 2021

Commented: Star Strider
on 14 May 2021

Hello,

I am tring to fill the area between the two ellipses. I am importing the values from Excel to plot the figure. After that I would like to fill the two ellipses with two different colors. How do I do that?.

CODE:

clc

clear all

load Phase12.mat

x = Phase1.x;

y = Phase1.z;

plot(x,y);

fill(x,y,'g');

hold on

a = Phase1.a;

b = Phase1.b;

plot(a,b);

% fill(a,b,'q')

x,y,a and b are the names of the four columns that I have imported from excel. x,y represents the bigger ellipse. I tried using some of the patch codes, but it did not work out. Can someone please help me with this?

Star Strider
on 16 Feb 2021

Try something like this:

t = linspace(0, 2*pi);

x = ([1;2]*cos(t)).'; % Produces (Nx2) Matrix

y = ([1;2]*sin(t)).'; % Produces (Nx2) Matrix

figure

patch([x(:,2); flipud(x(:,1))], [y(:,2); flipud(y(:,1))], 'g', 'EdgeColor','none') % Create ‘patch’ Object

hold on

plot(x(:,1), y(:,1), '-r') % Circle Outlines

plot(x(:,2), y(:,2), '-r') % Circle Outlines

hold off

axis('equal')

If your data are row vectors, do this:

patch([x fliplr(a)], [y fliplr(b)], 'g')

If they are column vectors:

patch([x; flipud(a)], [y; flipud(b)], 'g')

That should work.

Shri Harsha Bharadwaj
on 16 Feb 2021

Hello,

Thanks for the quick reply. I am a beginner in MATLAB. So, in the code that you have written above where does my data come into play. It does not right?. What you have in the top is just a similar example where you have used two equations to generate the circles. How do I generate something similar using my data?

Also adding

patch([x; flipud(a)], [y; flipud(b)], 'g')

to the code I have above, gives me the image attached.

while

patch([x fliplr(a)], [y fliplr(b)], 'g')

does not change anything.

Do you think there is some issue while exporting my data from EXCEL?. Its just 4 columns of data ( x,y,a and b respectively).

Star Strider
on 16 Feb 2021

My code should work.

Attach your data so I can see what the problem is with it.

Shri Harsha Bharadwaj
on 16 Feb 2021

Hello,

Thanks again for the quick response. Attached is the excel sheet that I import to create the plots.

Star Strider
on 17 Feb 2021

My pleasure!

There are NaN values in some of your data. That was the problem.

This works:

T1 = readtable('3Phase1.csv', 'VariableNamingRule','preserve');

xz = [T1.x T1.z]; % Concatenate Vectors

xz = xz(~any(isnan(xz),2),:); % Remove ‘NaN’ Rows

ab = [T1.a T1.b]; % Concatenate Vectors

ab = ab(~any(isnan(ab),2),:); % Remove ‘NaN’ Rows

figure

patch([xz(:,1); flipud(ab(:,1))], [xz(:,2); flipud(ab(:,2))], 'g', 'EdgeColor','none') % Create ‘patch’ Object

hold on

plot(xz(:,1), xz(:,2), '-b') % Curve Outlines

plot(ab(:,1), ab(:,2), '-b') % Curve Outlines

hold off

axis('equal')

producing:

It is easier to concatenate the vectors into their respective matrices in order to remove the NaN values.

Other than removing the NaN values and adapting my code to your variables, my original code is essentially unchanged.

Shri Harsha Bharadwaj
on 17 Feb 2021

Thank you Sir. I really appreciate the help.

I just had one more question. If I wanted to color the smaller ellipse in the center, would I have to do something similar to this?

Again, I cannot tell you how helpful this has been. I will look some of these commands up in detail to understand what actually is happening in the code.

Star Strider
on 17 Feb 2021

My pleasure!

‘If I wanted to color the smaller ellipse in the center, would I have to do something similar to this?’

Yes, however it would be easier. To fill the inner curve, simply add this patch call:

patch(ab(:,1), ab(:,2), 'r') % Fill Inner Curve

so the slightly revised, complete code is now:

T1 = readtable('3Phase1.csv', 'VariableNamingRule','preserve');

xz = [T1.x T1.z]; % Concatenate Vectors

xz = xz(~any(isnan(xz),2),:); % Remove ‘NaN’ Rows

ab = [T1.a T1.b]; % Concatenate Vectors

ab = ab(~any(isnan(ab),2),:); % Remove ‘NaN’ Rows

figure

patch([xz(:,1); flipud(ab(:,1))], [xz(:,2); flipud(ab(:,2))], 'g', 'EdgeColor','none') % Create ‘patch’ Object

hold on

plot(xz(:,1), xz(:,2), '-b') % Curve Outlines

plot(ab(:,1), ab(:,2), '-b') % Curve Outlines

patch(ab(:,1), ab(:,2), 'r') % Fill Inner Curve

hold off

axis('equal')

producing:

Of course, choose whatever line colours and inner-curve fill colour you want.

Shri Harsha Bharadwaj
on 17 Feb 2021

Hello Sir,

Thanks a lot for all the help you have provided me with this. I really appreciate it. It works perfectly. I will make some modifications with respect to the colours.

Is it okay if I keep the question open, so that I can ask you if I have any more questions?

Thanks a ton again.

Star Strider
on 17 Feb 2021

My pleasure!

It will be open as long as you want it to be. I will continue to respond as best I can.

Meanwhile, it appears that I did solve the problem as originally posted. If my Answer helped you solve your problem, please Accept it!

.

Shri Harsha Bharadwaj
on 17 Feb 2021

Thanks a lot. I will do the same.

Star Strider
on 17 Feb 2021

As always, my pleasure!

Thank you!

Shri Harsha Bharadwaj
on 21 Mar 2021

Hope you are doing well. I have been trying to remove the empty cells when plotting, but I have been having some issues.

When I plot the graph in Excel, I get the image attached below.

But when I import the same thing and try to plot it in MATLAB, I see a line.

I have been using your code from before. Can you please help me out with this?

clc

clear all

T1 = readtable('0.1399s.xlsx', 'VariableNamingRule','preserve');

YZ = [T1.Y T1.Z];

YZ = YZ(~any(isnan(YZ),2),:);

figure

plot(YZ(:,1),YZ(:,2),'b')

patch(YZ(:,1),YZ(:,2),'b')

Star Strider
on 21 Mar 2021

The problem is that the ‘x’ coordinate ‘crosses over’ at that point, and nothing I can come up with (sort, sortrows, unique) fixes it.

This fixes the plot:

k = boundary(YZ(:,1), YZ(:,2), 0.95);

with:

patch(YZ(k,1),YZ(k,2),'b')

however I have no idea how to fix the underlying data. The boundary function was introduced in R2014b.

Shri Harsha Bharadwaj
on 22 Mar 2021

Star Strider
on 22 Mar 2021

As always, my pleasure!

Shri Harsha Bharadwaj
on 23 Mar 2021

Sir, if I wanted to compute the area of the irregular shape seen above, how would I do it?

Most of what I found have something to do with image processing, which I believe is not required for me, as my shape is already on the axes.

polyarea() gives me area for a polygon. I could not find anything for my case. Can you please help me out with this?

It tried using area(), but that does not seem to work either.

Star Strider
on 23 Mar 2021

Both trapz and polyarea work here, with reapect to the boundary call result. The area function would not work here because it produces an area plot.

The area given by trapz:

Area = trapz(YZ(k,1),YZ(k,2))

Area =

-9.872788536391784e-07

and by polyarea:

AreaP = polyarea(YZ(k,1),YZ(k,2))

AreaP =

9.872788536391784e-07

differ only in the sign of the result. The magnitudes are the same.

(I would not use the original data because of the sign-reversal in ‘YZ(:,1)’ that caused problems for the patch call.)

Choose whichever result works best in your application.

Shri Harsha Bharadwaj
on 26 Mar 2021

Thanks for the help sir. I apologize for the late reply.

Star Strider
on 26 Mar 2021

As always, my pleasure!

Shri Harsha Bharadwaj
on 5 Apr 2021

@Star Strider Hello sir,

I am trying to compute the perimeter of these irregular shapes. I came across a reference code to do that.

% Contour coordinates

A=xlsread('justplastic0.5.xlsx','A1:B100');

% Compute perimeter

N=size(A,1);

D=A(:,2:N)-A(:,1:(N-1));

D=sqrt(sum(D.^2,2)); % edge lenghts

P=sum(D); % perimeter

But I get wrong numbers with the code above.

Essentially, I am trying to figure out the Circularity of these blobs. Hence, I am trying to compute the area and perimeter.

I apologize that I keep coming back with more questions. I can create seperate questions from now on, if that is better. Is there a way I can club the code above with the one that you wrote, so that I can get everything with a single code.

Star Strider
on 5 Apr 2021

I do not understand what that code is supposed to do.

Also, what are the data in ‘planeat11mm.xlsx’? There are 4 columns, ‘x’, ‘z’, ‘a’, and ‘b’. What should be done with them?

Shri Harsha Bharadwaj
on 5 Apr 2021

justplastic0.5.xlsx, has just one set of data. So, I would like to compute the perimeter of just one blob.

But in the case of planeat11mm.xlsx, there are two sets of data. I would like to compute the perimeter of both the inner shape and the outer shape.

Shri Harsha Bharadwaj
on 5 Apr 2021

Star Strider
on 6 Apr 2021

It turns out that I made a stupid mistake in my code, and multiplied instead of addied.

That is now corrected, and this works, giving reasonable results:

T1 = readtable('justplastic0.5.xlsx');

T2 = readtable('planeat11mm.xlsx');

pwr10 = @(x) [x/10^fix(log10(x)-1) fix(log10(x)-1)];

x1 = T1.z;

y1 = T1.x;

dx1 = gradient(x1);

dy1 = gradient(y1);

C1 = trapz(sqrt(dx1.^2 + dy1.^2));

figure

plot(x1, y1)

grid

axis('equal')

text(mean(T1.z), mean(T1.x), sprintf('$C = %.3f\\times 10^{%d}$', pwr10(C1)), 'Horiz','center','Vert','middle', 'Interpreter','latex')

x2 = T2.z(~isnan(T2.z));

y2 = T2.x(~isnan(T2.x));

dx2 = gradient(x2);

dy2 = gradient(y2);

C2 = trapz(sqrt(dx2.^2 + dy2.^2));

figure

plot(T2.z,T2.x)

grid

axis('equal')

text(mean(x2), mean(y2), sprintf('$C = %.3f\\times 10^{%d}$', pwr10(C2)), 'Horiz','center','Vert','middle', 'Interpreter','latex')

x3 = T2.a;

y3 = T2.b;

dx3 = gradient(x3);

dy3 = gradient(y3);

C3 = trapz(sqrt(dx3.^2 + dy3.^2));

figure

plot(x3, y3)

grid

axis('equal')

text(mean(T2.a), mean(T2.b), sprintf('$C = %.3f\\times 10^{%d}$', pwr10(C3)), 'Horiz','center','Vert','middle', 'Interpreter','latex')

The circumference prints out in the centre of each figure in the plot. (The ‘pwr10’ function just creates an interesting string for the plots. It may not be robust, and is not required for the code.)

Since the first data set is a roughly circular plot, I checked that calculation with:

x11 = T1.z;

y11 = T1.x;

x11 = x11 - mean(x11);

y11 = y11 - mean(y11);

f = @(r) ((x11).^2 + (y11).^2 - r^2);

r = fsolve(f, 1E-4);

C11 = 2*pi*r;

That gives essentially the same result as ‘C1’.

I almost cannot imagine that I spent several hours on this and in all that time completely failed to notice that I multiplied instead of added in the trapz calls. With that correction , it works!

Shri Harsha Bharadwaj
on 6 Apr 2021

Star Strider
on 6 Apr 2021

As always, my pleasure!

Shri Harsha Bharadwaj
on 6 Apr 2021

Star Strider
on 6 Apr 2021

As always, my pleasure!

The time I spent was my fault! It actually should only have required a few minutes.

Shri Harsha Bharadwaj
on 27 Apr 2021

Hope you are doing well sir. I had one last question that I wanted to run by you.

Given an irregular figure, it is possible to calculate the centerline distance from the leading end to the trailing end.

The red line illustrates the centerline length that I want to compute. I made the centerline in word. Is there a way we can generate a centerline like this automatically in MATLAB and calculate the length of the centerline?. Even an approximation would be good, especially in the curvature part.

I have also attached the excel file for your reference.

Thanks for the help.

Star Strider
on 27 Apr 2021

I worked on that for a couple hours. I have absolutely no idea how to draw that centrelline, and I cannot find any functions that would do it.

Sorry.

Shri Harsha Bharadwaj
on 27 Apr 2021

@Star Strider No problem sir. Thanks for the help. Have a great day.

Shri Harsha Bharadwaj
on 13 May 2021

Hope you are doing well. This might be a very simple question for you.

I am trying to 3D plot something like this.

Given I have the data, how would I plot this?. Most of the codes that I looked up for 3D plotting uses some sort of an equation in one of the variables.

https://www.mathworks.com/help/matlab/visualize/creating-3-d-plots.html - Should I be using the surf function?

EDIT:

I looked into your older answers - https://www.mathworks.com/matlabcentral/answers/829218-3d-plot-from-excel#answer_699128?s_tid=prof_contriblnk

But not the same.

Star Strider
on 13 May 2021

I wish it was simple! I have no idea how to plot them because I have no idea what the variables are.

My best effort —

C1 = readcell('https://www.mathworks.com/matlabcentral/answers/uploaded_files/617108/test1.csv');

T1 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/617108/test1.csv', 'VariableNamingRule','preserve');

First10Rows = T1(1:10,:)

First10Rows = 10×7 table

figure

plot3(T1{:,3},T1{:,5},T1{:,2})

hold on

plot3(T1{:,3},T1{:,5},T1{:,6})

hold off

grid on

The arguments to the plot3 call refer to the columns of ‘T1’, so ‘T1{:,1}’ is the first column, and so for the others.

I added the readcell call because readtable read in one of the variables as NaN and I wanted to see what that was. It turns out to apparently be a vector of empty cells.

Shri Harsha Bharadwaj
on 13 May 2021

@Star Strider Thanks for the help sir. I just had one question. The Z axis values are different. Could ou please tell as as to why they are being plotted at the same location?

EDIT:

Also ,when I have varying Y as well, all the Y points seem to originate from the same location. When I plot the graph in excel, I have something like this.

But when I do the same in MATLAB. both the Y and Z values do not vary. I am not sure if how I said this makes much sense. The bounds on the blue line is between [-1,1]* 10^-3, whereas the bounds of the orange line is different. But when plotted in MATLAB, they both have the same bounds of [-1,1]*10^-3.

Star Strider
on 13 May 2021

As always, my pleasure!

They are plotted in the same location because I have no idea what the variables are, or how you would like them plotted. The ‘Velocity’ variables are obviously the dependent variables, however I have no idea what the independent variables should be. (The plot3 function requires two independent variables and one dependent variable.)

Perhaps —

T1 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/617108/test1.csv', 'VariableNamingRule','preserve');

First10Rows = T1(1:10,:)

First10Rows = 10×7 table

figure

plot3(T1{:,1},T1{:,3},T1{:,2})

hold on

plot3(T1{:,5},T1{:,7},T1{:,6})

hold off

grid on

would work instead.

Shri Harsha Bharadwaj
on 13 May 2021

Thanks for the help sir. I was testing it out. Thanks a ton again.

Star Strider
on 14 May 2021

As always, my pleasure!

Apparently the data file changed (I have been away doing other things), since it is not the same one I initially plotted.

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!Unable to complete the action because of changes made to the page. Reload the page to see its updated state.

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

Select web siteYou can also select a web site from the following list:

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

- América Latina (Español)
- Canada (English)
- United States (English)

- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)

- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)