MATLAB Answers

How To Plot Data From A Cell Array Containing (x,y) Coordinates?

74 views (last 30 days)
Niv Zuckerman
Niv Zuckerman on 4 May 2018
Answered: Wick on 5 May 2018
Hi there,
I'm starting to build a vlm (Vortix Lattice Method) and in order to do so i have to divide a given area to several trapezoids.I'm using a cell array to store my data points (after the division) but I'm facing a problem plotting those points.
tl;dr - I have a 3x5 cell array and each cell is a 1x2 double matrix containing (x,y) coordinates. I Want to plot all of the points on the same graph.
Bonus Help!,
My next Stage is to draw lines connecting each point to the surrounding points in order to achieve sort of a net consists of my calculated points. Is there an easy way to do so?
Nx = 5;
Ny = 3;
Y_frame = [0 -1.5 -3 -2 0];
X_frame = [0 5 5 0 0];
xlim([min(X_frame-1) max(X_frame+1)]);
ylim([min(Y_frame-1) max(Y_frame+1)]);
hold on
Y_frame = Y_frame(1:4);
X_frame = X_frame(1:4);
x_dist = linspace(min(X_frame),max(X_frame),Nx);
P_upper = linediv([X_frame(1),Y_frame(1)],[X_frame(2),Y_frame(2)],Nx);
P_lower = linediv([X_frame(3),Y_frame(3)],[X_frame(4),Y_frame(4)],Nx);
A = cell(Ny,Nx);
for i=1:Nx
A(1,i) = {P_upper(:,i)'};
A(Ny,i) = {P_lower(:,i)'};
P_i = linediv(cell2mat(A(1,i)),cell2mat(A(end,i)),Ny);
for j=2:Ny-1
A(j,i) = {P_i(:,j)'};
Here's my code, A is the cell array i wish to plot.
Thanks in advance
Niv Zuckerman
Niv Zuckerman on 4 May 2018
Oh I'm sorry, must have forgotten to input this:
function [ line ] = linediv(a,b,N)
% linediv is used to divide a straight line (defined by two points)
% into N points, using his linear equation
% a & b are the edge of the line
% N is the number of evenly spaced points to return
% line is an array containing the desired points (2d)
line(1,:) = linspace(a(1),b(1),N);
line(2,:) = linspace(a(2),b(2),N);

Sign in to comment.

Answers (1)

Wick on 5 May 2018
Niv, I assume you're putting things into a cell array on purpose. That's an unnecessary step since each element is the same shape (1,2) and they're all numbers. You could just as easily have populated a matrix like the XY matrix that results below.
Add these few lines after your code and it will plots the values in A. Given the bonus question above, I don't think A is doing what you think it's doing but perhaps I'm wrong. At least you'll be able to troubleshoot by seeing the values of A now.
XY = cell2mat(A);
X = XY(:,1:2:end);
Y = XY(:,2:2:end);
hold off

Community Treasure Hunt

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

Start Hunting!