필터 지우기
필터 지우기

How do you save for loop plots to the correct directory?

조회 수: 3 (최근 30일)
Macy
Macy 2023년 2월 5일
편집: Macy 2023년 2월 5일
%Hello, this is very simple code but I am completey new to MatLab. Help!
points = [1:3]
points = 1×3
1 2 3
color = ["r", "g", "b", "m", "b"];
% Create a string variable that contains the name of the directory you
% created where you will writeout the plots
Outputfigures ='C:\Users\sfai\Documents\MATLAB\OutputFigures'; %I am not sure if this is what is meant by directory;
for i=1:3
h=figure
plot(points,points,color(i))
xlabel('X')
ylabel('Y')
title(strcat({'Figure # '}, num2str(i)))
%saveas(h,sprintf('OutputFigures/FIG%d.png',i));
%^^^This saves the titles with the correct names, but I was
%instructed to use a method that "concatenates strings to create the correct file and folder path"
%saveas(gcf, [strcat('Figure #', num2str(i), '.png')]);
%^^^This saves the titles with the correct names, but it saves
%it to the current folder.
%So what's the best way to save these figures with the correct names and file location?
end
h =
Figure (1) with properties: Number: 1 Name: '' Color: [1 1 1] Position: [671 661 577 433] Units: 'pixels' Show all properties
h =
Figure (2) with properties: Number: 2 Name: '' Color: [1 1 1] Position: [671 661 577 433] Units: 'pixels' Show all properties
h =
Figure (3) with properties: Number: 3 Name: '' Color: [1 1 1] Position: [671 661 577 433] Units: 'pixels' Show all properties

채택된 답변

Shashank Pathrudkar
Shashank Pathrudkar 2023년 2월 5일
편집: Shashank Pathrudkar 2023년 2월 5일
points = [1:3]
color = ["r", "g", "b", "m", "b"];
Outputfigures ='C:\Users\sfai\Documents\MATLAB\OutputFigures\';
for i=1:3
figure
plot(points,points,color(i))
xlabel('X')
ylabel('Y')
title(strcat({'Figure # '}, num2str(i)))
saveas(gcf,strcat(Outputfigures,num2str(i),'.fig')); % saving as a MATLAB figure file
print(gcf,strcat(Outputfigures,num2str(i),'.eps'),'-depsc'); % saving in eps format
print(gcf,strcat(Outputfigures,num2str(i),'.pdf'),'-depsc'); % saving in pdf format
print(gcf,strcat(Outputfigures,num2str(i),'.png'),'-dpng'); % saving as a png
end
  댓글 수: 1
Macy
Macy 2023년 2월 5일
편집: Macy 2023년 2월 5일
Thank you, I had issues with my string variable of the folder I wanted to save it in, when I did Outputfigures = 'Outputfigures/';, it works!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by