How to simply this script to create figures?
조회 수: 5 (최근 30일)
이전 댓글 표시
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
채택된 답변
Walter Roberson
2017년 1월 8일
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
Walter Roberson
2017년 1월 9일
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.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Graphics Object Programming에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!