How to simply this script to create figures?
6 views (last 30 days)
Show older comments
Hsinho Huang
on 8 Jan 2017
Commented: Walter Roberson
on 9 Jan 2017
I prepared the following script to create two figures. Each figure has 2-by-6 subplots. The only difference between them is that the first one uses a plot function and the second one uses a semilogy function. Otherwise, legend, xlabel, ylabel, and position are the same. Also, I want to export the figures as Fig1.eps and Fig2.eps files. How can I further simply this script to avoid repeated codes?
harray(1)=figure('Name',fname{1});
for k1 = 1:12
h{k1} = subplot(2,6,k1)
hLine{k1} = plot(X,Y(k1,:),X,Z(k1,:));title(type{k1});
end
legend('test1','test2');
xlabel(h{7},'\fontname{Helvetica}X');
xlabel(h{8},'\fontname{Helvetica}X');
xlabel(h{9},'\fontname{Helvetica}X');
xlabel(h{10},'\fontname{Helvetica}X');
xlabel(h{11},'\fontname{Helvetica}X');
xlabel(h{12},'\fontname{Helvetica}X');
ylabel(h{1},'\fontname{Helvetica}Y');
ylabel(h{7},'\fontname{Helvetica}Y');
set(harray(1), 'Position', [0 0 1366 768]);
print -depsc Fig1
harray(2)=figure('Name',fname{2});
for k1 = 1:12
h{k1} = subplot(2,6,k1)
hLine{k1} = semilogy(X,Y(k1,:),X,Z(k1,:));title(type{k1});
end
legend('test1','test2');
xlabel(h{7},'\fontname{Helvetica}X');
xlabel(h{8},'\fontname{Helvetica}X');
xlabel(h{9},'\fontname{Helvetica}X');
xlabel(h{10},'\fontname{Helvetica}X');
xlabel(h{11},'\fontname{Helvetica}X');
xlabel(h{12},'\fontname{Helvetica}X');
ylabel(h{1},'\fontname{Helvetica}Y');
ylabel(h{7},'\fontname{Helvetica}Y');
set(harray(2), 'Position', [0 0 1366 768]);
print -depsc Fig2
0 Comments
Accepted Answer
Walter Roberson
on 8 Jan 2017
Delete the second half of the code. After you print the first figure, do
cellfun(@(axes_handle) set(axes_handle, 'YScale', 'log'), h);
Then you can print figure 1 again.
That is, the difference between regular plot and semilogy is that in semilogy plot, the YScale property is set to 'log'. The cellfun is there because you happened to use cells to store the axes handles; you could have used a plain arrays instead for your h and hLine .
2 Comments
Walter Roberson
on 9 Jan 2017
a)
set(gcf, 'Name', fname{2})
b) You could create a function in which you pass in the data to be plotted and pass in either 'normal' or 'log' to be set as the YScale
c) You should be able to pass in the figure the way you show in the first position. You would not use the 'fig' option in that case.
However, the modification that I show changes the existing figure rather than creating a new figure. The graphing work can be reduced by using copyobj() to clone the figure and then modifying the cloned figure, but at that point it is probably easier to create a small function instead.
More Answers (0)
See Also
Categories
Find more on Graphics Object Programming in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!