Marker Indices automation using 'set' function

20 views (last 30 days)
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.

Accepted Answer

Steven Lord
Steven Lord on 6 Dec 2021
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 Comments
KarolN
KarolN on 6 Dec 2021
Edited: KarolN on 6 Dec 2021
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 on 7 Dec 2021
Forgot to accept your answer, sorry I rectified this.

Sign in to comment.

More Answers (0)

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!