Drawing lines with a handle

18 views (last 30 days)
Carly Hudson
Carly Hudson on 5 Mar 2022
Commented: Stephen23 on 5 Mar 2022
Hello. I am working on a project and I am having a hard time plotting a "line for each link". I have tried the plot and line function but I cannot seem to get them to plot and I keep getting errors like: "Error using line
Invalid data argument." or "Error using plot
Invalid data argument." If you could offer me any support on how to start figurimg out how to plot this I would greatly appreciate it.
function l = draw_links(link_set,link_colors,ax)
% Draw a set of lines for a link structure into a specified axis
%
% Inputs:
%
% link_set: A 1xn cell array, each entry of which is a matrix whose
% columns are the endpoints of the lines describing one link of the
% system (as constructed by planar_build_links or
% planar_build_links_prismatic
% link_colors: 1x3 cell array. Each entry can be either a standard matlab
% color string (e.g., 'k' or 'r') or a 1x3 vector of the RGB values
% for the color (range from 0 to 1)
% ax: The handle to an axis in which to plot the links
%
% Output:
%
% l: A cell array of the same size as link_set, in which each entry is a
% handle to a line structure for that link
%%%%%%%%
% Start by creating an empty cell array of the same size as link_set,
% named 'l'
l = cell(link_set);
%%%%%%%%
% Draw a line for each link, with circles at the endpoints, and save
% the handle for this line in the corresponding element of 'l'
link_colors = {'k','r','g'};
l{1} = plot(link_set,ax);
end
  1 Comment
Stephen23
Stephen23 on 5 Mar 2022
"Output... A cell array of the same size as link_set, in which each entry is a handle to a line structure for that link"
One graphics object array would probably be simpler to work with:

Sign in to comment.

Accepted Answer

Simon Chan
Simon Chan on 5 Mar 2022
Note that link_colors is one of the input argument in function draw_links, so there is no need to define it inside the function as well.
The axis ax should be the first argument in function plot and hence you get the error of Invalid data argument.
Following shows an example and you may modify your own code accordingly.
link_set{1} = [1:10; 1:10]';
link_set{2} = [2:2:20; 2:2:20]';
figure;
ax = gca;
draw_links(link_set,ax);
h = 1×2 cell array
{2×1 Line} {2×1 Line}
function h = draw_links(link_set,ax)
Nz = length(link_set);
h = cell(1,Nz);
for k = 1:Nz
h{1,k} = plot(ax,link_set{k});
hold(ax,'on');
end
hold(ax,'off');
h
end
  2 Comments
Carly Hudson
Carly Hudson on 5 Mar 2022
is there a way to do this with libe funnction instead of plot? i have to use line and i dont really understand the difference between the two
Simon Chan
Simon Chan on 5 Mar 2022
Here is using function line and also added the line color.
Notice that there are only three colors in variable link_colors, so the code will gives an error when there are 4 lines to be plotted on the figure.
link_set{1} = [1:10; 1:10]';
link_set{2} = [11:20; 11:20]';
link_colors = {'k','r','g'};
figure;
ax = gca;
draw_links(link_set,link_colors,ax);
h = 1×2 cell array
{1×1 Line} {1×1 Line}
function h = draw_links(link_set,link_colors,ax)
Nz = length(link_set);
h = cell(1,Nz);
for k = 1:Nz
h{1,k} = line(ax,link_set{k}(:,1),link_set{k}(:,2),'Color',link_colors{k});
hold(ax,'on');
end
h
end

Sign in to comment.

More Answers (0)

Categories

Find more on Discrete Data Plots 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!