How do I plot a series of points as a collection of 1x2 arrays?
23 views (last 30 days)
Show older comments
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.
5 Comments
Sindar
on 24 Mar 2020
You could use varargin:
function plotPoints(varargin);
%insert code here;
end
which will produce a cell array as I describe in my answer (in the variable varargin instead of points)
Answers (1)
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,[]);
0 Comments
See Also
Categories
Find more on Annotations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!