Drawing lines with a handle

19 次查看(过去 30 天)
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 个评论
Stephen23
Stephen23 2022-3-5
"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:

请先登录,再进行评论。

采纳的回答

Simon Chan
Simon Chan 2022-3-5
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 个评论
Carly Hudson
Carly Hudson 2022-3-5
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 2022-3-5
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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Specifying Target for Graphics Output 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by