Marker Indices automation using 'set' function

19 次查看(过去 30 天)
I have a script with a lot of plotting, and this involves markers which, howewer, have same parameters all over. Now, is there any way to streamline the code, so it doesn't have to repeat these lines all over and over again?
h = plot(49,8.4745,'o-','MarkerFaceColor','blue','MarkerEdgeColor','blue','MarkerIndices',1);
h = plot(50,21.4411,'o-','MarkerFaceColor','blue','MarkerEdgeColor','blue','MarkerIndices',1);
h = plot(50,39.5221,'o-','MarkerFaceColor','blue','MarkerEdgeColor','blue','MarkerIndices',1);
h = plot(50,76.2767,'o-','MarkerFaceColor','blue','MarkerEdgeColor','blue','MarkerIndices',1);
I tried to rewrite it into this
h1 = plot(x1,y1);
h2 = plot(x2,y2);
h3 = plot(x2,y3);
h4 = plot(x2,y4);
set([h1 h2 h3 h4],'o-','MarkerFaceColor','blue','MarkerEdgeColor','blue','MarkerIndices',1)
But I keep getting an error
Error using matlab.graphics.chart.primitive.Line/set
Invalid or deleted object.

采纳的回答

Steven Lord
Steven Lord 2021-12-6
What you've written second would almost work if you turned hold on. However you can't use a line specification in a set call like you did. If you replaced 'o-' with 'Marker', 'o', 'LineStyle', '-' it should work.
x = 0:360;
h1 = plot(x, sind(x));
hold on
h2 = plot(x, sind(2*x));
set([h1 h2], 'Marker', 'o', 'MarkerIndices', 1:45:361)
Or you could write a function handle and use that function handle with your data as input.
plotMe = @(x, y) plot(x, y, 'Marker', 'o', 'MarkerIndices', 1:45:361);
figure
x = 0:360;
plotMe(x, sind(x));
hold on
plotMe(x, sind(2*x));
  2 个评论
KarolN
KarolN 2021-12-6
编辑:KarolN 2021-12-6
OK so I shortened the code and now I have:
h1 = plot(x1,y1);
h2 = plot(x2,y2);
h3 = plot(x2,y3);
h4 = plot(x2,y4);
set([h1 h2 h3 h4], 'Marker', 'o', 'LineStyle', '-','MarkerFaceColor','blue','MarkerEdgeColor','blue','MarkerIndices',1)
howewer I still have to repeat it in every plot chart. I keep wondering, how to implement all this in one function, and then only insert this function into my plot charts.
I tried with this
x1 = 49;
x2 = 50;
y1 = 8.4745;
y2 = 21.4411;
y3 = 39.5221;
y4 = 76.2767;
h1 = plot(x1,y1);
h2 = plot(x2,y2);
h3 = plot(x2,y3);
h4 = plot(x2,y4);
PlotMe = [h1, h2, h3, h4];
set(PlotMe, 'Marker', 'o', 'LineStyle', '-','MarkerFaceColor','blue','MarkerEdgeColor','blue','MarkerIndices',1)
But somehow I got an error regardless.
Coordinates for markers are the same for all charts, just like their color and shape
KarolN
KarolN 2021-12-7
Forgot to accept your answer, sorry I rectified this.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Lighting, Transparency, and Shading 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by