How to plot and export two figures by using a "for" loop?

3 visualizaciones (últimos 30 días)
Giuseppe
Giuseppe el 4 de Oct. de 2022
Comentada: Image Analyst el 14 de Oct. de 2022
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.

Respuesta aceptada

Image Analyst
Image Analyst el 4 de Oct. de 2022
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 comentarios
Giuseppe
Giuseppe el 13 de Oct. de 2022
Editada: Giuseppe el 14 de Oct. de 2022
Hi @Image Analyst, can you kindly show how to align both vertically and horizontally the textbox with the legend box?
Image Analyst
Image Analyst el 14 de Oct. de 2022
Get the handle from legend and annotation, then set the position property of them to match
ha = annotation(
hl = legend(
etc.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Lighting, Transparency, and Shading en Help Center y File Exchange.

Productos


Versión

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by