How to plot and export two figures by using a "for" loop?
3 ビュー (過去 30 日間)
古いコメントを表示
Hello everyone! In my script I have many duplicated section used to plot couple of figures whose codes are parametrized for couples of numbers, i.e. I have a database for two different cases and I have to produce and then export as .pdf images couples of images that differes from each other (inside the couple only for one parameter).
Here there is an example code to let you understand my previous lines:
clear all; close all; clc;
x1 = linspace(1,100,100);
x2 = linspace(2,200,100);
X = [x1;x2];
% for i = 1:2
% First plot
fig=figure;
plot(X(1,:),cos(X(1,:)),'Color',[0 0.44 0.36]);
legend('function 1');
xlabel('x_1');
ylabel('y_1');
dim = [.168 .85 .275 .06];
str = ['annotation box no.',num2str(numel(x1(2))),];
annotation('textbox',dim,'String',str,'Interpreter',"latex",'FitBoxToText','off',...
'BackgroundColor',[1 1 1],'FontSize',9);
grid on;
set(gcf,'position',[278.6, 0.2, 600, 400]); %aspect ratio 3/2
%exportgraphics(fig,'fig_test1.pdf','ContentType','vector'); %salva
% Second plot
fig=figure;
plot(X(2,:),cos(X(2,:)),'Color',[0.2 0.24 0.46]);
legend('function 2')
xlabel('x_2');
ylabel('y_2');
str = ['annotation box no.',num2str(numel(x2(1))),];
annotation('textbox',dim,'String',str,'Interpreter',"latex",'FitBoxToText','off',...
'BackgroundColor',[1 1 1],'FontSize',9);
grid on;
set(gcf,'position',[278.6, 0.2, 600, 400]); %aspect ratio 3/2
%exportgraphics(fig,'fig_test2.pdf','ContentType','vector'); %salva
% end
As you can see, the differences are about the indices 1 and 2 related to the variables to plot.
I would to get the same results you can see in my example code, i.e. two separated figures in .mlx Matlab file and then two separated .pdf images, but using a "for" loop so as to reduce lines of code and lighten Matlab runs. Can you help me to code using "for"loop to get my goals?
Thanks in advance.
0 件のコメント
採用された回答
Image Analyst
2022 年 10 月 4 日
Try this:
% Initialization Steps.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 18;
markerSize = 40;
x1 = linspace(1,100,100);
x2 = linspace(2,200,100);
X = [x1;x2];
plotColors = [0 0.44 0.36; 0.2 0.24 0.46]
for k = 1:2
% First plot
hFigure = figure;
plot(X(k,:),cos(X(k,:)),'Color',plotColors(k, :));
str = sprintf('Function %d', k)
legend(str);
str = sprintf('Function_%d', k)
title(str, 'Interpreter','none', 'FontSize', fontSize);
str = sprintf('x_%d', k)
xlabel(str, 'FontSize', fontSize);
str = sprintf('y_%d', k)
ylabel(str, 'FontSize', fontSize);
% Make an annotation box.
dim = [.168 .85 .275 .06];
str = sprintf('annotation box number %d', k);
annotation('textbox',dim,'String',str,'Interpreter',"latex",'FitBoxToText','off',...
'BackgroundColor',[1 1 1],'FontSize',9);
grid on;
% Set plot size and location.
set(gcf,'Position',[278.6, 0.2, 600, 400]); %aspect ratio 3/2
drawnow;
% Save it to disk
fileName = fullfile(pwd, sprintf('Fig_test %d.pdf', k));
fprintf('Please wait. Saving ""%s!\n', fileName)
exportgraphics(hFigure, fileName, 'ContentType', 'vector'); %salva
end
fprintf('Done!\n')
2 件のコメント
Image Analyst
2022 年 10 月 14 日
Get the handle from legend and annotation, then set the position property of them to match
ha = annotation(
hl = legend(
etc.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Lighting, Transparency, and Shading についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!