Effacer les filtres
Effacer les filtres

How to simply this script to create figures?

4 vues (au cours des 30 derniers jours)
Hsinho Huang
Hsinho Huang le 8 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

Réponse acceptée

Walter Roberson
Walter Roberson le 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 commentaires
Hsinho Huang
Hsinho Huang le 8 Jan 2017
Modifié(e) : Hsinho Huang le 8 Jan 2017
Thanks! I have following questions.
a. In the suggested code, the title of the figure 2 is inherited from figure 1 without changing. How can I change it?
b. In the case that both figures use the same plot function to plot different data (i.e. Y1 and Z1 for figure 1; Y2 and Z2 for figure 2), is there any chance to simply this script?
c. I assigned the two figures as harray(1) and harrary (2). How can I print them into eps files later? This answer will be useful for me if I have 18 figures and print them at once after generating figures. I tried this script but failed. How can I code it properly?
for i=1:18
print(harray(i),'-depsc','Fig',num2str(i)')
end
Walter Roberson
Walter Roberson le 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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Graphics Object Programming dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by