3 views (last 30 days)

I need to write a function that will plot a series of points that are input as 1x2 arrays separated by commas. I understand that one could simply input all of the x values into one array and all of the y values in to an array (ie. x = [1 4 3 7 2] y = [2 5 1 2 8] plot(x,y,'*'),) however for this problem i am required to input the points as 1x2 arrays separated by collumns like so:

function plotPoints(points);

%insert code here;

end

then in a new script:

plotPoints([1,2] , [2,6] , [8,3] , [6,9])

I have no idea how to go about doing this.

Sindar
on 24 Mar 2020

If you have a cell array of 1x2 arrays like so:

points = {[1,2] , [2,6] , [8,3] , [6,9]};

then you can turn it into a 1x (2N) array, then reshape it to a 2xN array:

points = reshape(cell2mat(points),2,[]);

from there, use Sriram's answer (except the first row is x):

plot(points(1,:),points(2,:),'*')

If you start with a 1x(2N) array, you can remove the cell2mat() call:

points = reshape(points,2,[]);

Sign in to comment.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 5 Comments

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/512551-how-do-i-plot-a-series-of-points-as-a-collection-of-1x2-arrays#comment_814349

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/512551-how-do-i-plot-a-series-of-points-as-a-collection-of-1x2-arrays#comment_814349

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/512551-how-do-i-plot-a-series-of-points-as-a-collection-of-1x2-arrays#comment_814354

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/512551-how-do-i-plot-a-series-of-points-as-a-collection-of-1x2-arrays#comment_814354

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/512551-how-do-i-plot-a-series-of-points-as-a-collection-of-1x2-arrays#comment_814355

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/512551-how-do-i-plot-a-series-of-points-as-a-collection-of-1x2-arrays#comment_814355

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/512551-how-do-i-plot-a-series-of-points-as-a-collection-of-1x2-arrays#comment_814357

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/512551-how-do-i-plot-a-series-of-points-as-a-collection-of-1x2-arrays#comment_814357

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/512551-how-do-i-plot-a-series-of-points-as-a-collection-of-1x2-arrays#comment_814361

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/512551-how-do-i-plot-a-series-of-points-as-a-collection-of-1x2-arrays#comment_814361

Sign in to comment.