Is that possible to assemble several options and pass to the plot function in matlab

前端 未结 4 576
死守一世寂寞
死守一世寂寞 2021-01-20 00:58

I am using matlab to plot several figures and hope these figure use the same plot options. To be more specific, it looks something like this.

N = 20;
Fs = 20         


        
4条回答
  •  醉酒成梦
    2021-01-20 01:14

    A very clean alternative approach would be to keep the plot command as simple as possible and manipulate the handles afterwards.

    opts = {'Color','red','MarkerFaceColor', 'b', 'LineWidth',3};
    
    h(1) = plot(t, x);
    grid on;
    subplot(312);
    h(2) = plot(t, y);
    grid on;
    subplot(313);
    h(3) = plot(t, z);
    grid on;
    
    arrayfun(@(x) set(x,opts{:}),h)
    

    The advantage over the indeed neat approach by Nemesis is, that in case you have multiple sets of properties, like:

    opts.slimRed = {'Color','red','MarkerFaceColor', 'b', 'LineWidth',1};
    opts.fatBlue = {'Color','blue','MarkerFaceColor', 'b', 'LineWidth',5};
    

    and you want to exchange them, you just need to modify one variable

    arrayfun(@(x) set(x,opts.fatBlue{:}),h)
    

    to change the appearance of a whole set of handles h.

提交回复
热议问题