Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

plot

matlab

I am using MATLAB to plot several figures and hope these figure use the same plot options, it looks something like this:

N = 20;
Fs = 200;
t = (0:N-1)/Fs;

x = sin(2*pi*10*t);
y = cos(2*pi*20*t);
z = x + y;

figure(1),clf;
subplot(311);
plot(t, x, 'bs-', 'MarkerFaceColor', 'b', 'LineWidth', 3);
grid on;
subplot(312);
plot(t, y, 'bs-', 'MarkerFaceColor', 'b', 'LineWidth', 3);
grid on;
subplot(313);
plot(t, z, 'bs-', 'MarkerFaceColor', 'b', 'LineWidth', 3);
grid on;

You can see the plot options are exactly the same. If I want to change the style, I have to change each of them. Is that possible assemble/group them together and pass them to the plot function?

I have tried to put them in a cell like this plotOptions = {'bs-', 'MarkerFaceColor', 'b', 'LineWidth', 3}; It doesn't work. The reason might be the plot functions would take the plotOptions as one paramter and thus failed to parse it.

like image 516
shehperd Avatar asked Dec 02 '22 17:12

shehperd


2 Answers

Using a cell with the options was already a good approach. Just use {:}, as below:

opt = {'bs-', 'MarkerFaceColor', 'b', 'LineWidth', 3};
figure(1),clf;
subplot(311);
plot(t, x, opt{:});

Then, each element of the cell is evaluated as single argument.

like image 60
Nemesis Avatar answered Dec 21 '22 19:12

Nemesis


Solution with unique plotting function:

subplot(312);
myplot(t,y)

Save myplot function as a separate m-file.

function myplot(t,x)
    plot(t, x, 'bs-', 'MarkerFaceColor', 'b', 'LineWidth', 3);
end
like image 28
rozsasarpi Avatar answered Dec 21 '22 21:12

rozsasarpi