Save figures created on Matlab within a folder

2 views (last 30 days)
I have a series of images generated by a code, for example this one:
A = imread('Example.jpg');
B = imread('Example1.jpg');
C = imread('Example2.jpg');
I want to save these images inside a new folder while keeping the size and name.
I am using mkdir to create a new folder "NewFolder" within a specific folder:
parentdir = 'C:\Users\Alberto\Downloads';
ROOT_FOLDER = 'NewFolder';
newfolder = fullfile(parentdir, sprintf('%s%d', ROOT_FOLDER));
creation_new_folder = mkdir(newfolder);
I do not understand at this point how to save the images created inside this "NewFolder". I thought of something like this but, of course, it doesn't work:
Alberto Acri
Alberto Acri on 28 Oct 2022
Edited: Alberto Acri on 28 Oct 2022
@KALYAN ACHARJYA could you please point me to which question would be helpful to my case? I have seen many questions, but none apply to my case. I thank you.
I am currently using the following code:
close all
clear all
% figures already created in a previous code (there are more than 3, usually a variable number)
fig_A = imread('Example.jpg');
fig_B = imread('Example1.jpg');
fig_C = imread('Example2.jpg');
h(1) = image(fig_A);
h(2) = image(fig_B);
h(3) = image(fig_C);
destdirectory = 'C:\Users\Alberto\Downloads\NewFolder';
mkdir(destdirectory); % create the directory
I would like to save the generated images (fig_A, fig_B, fig_C), which actually in the code I am using are not numbered, inside the created folder "NewFolder".
I tried using these lines of code:
thisimage = 'NAME.jpg'; % name I want to give the figures (possibly like "fig_1", "fig_2", "fig_3", etc.)
fulldestination = fullfile(destdirectory, thisimage);
generated_figures = fig_B; % I would like to insert in this line the figures (in the example there are 3 figures) that I generated above
imwrite(generated_figures, fulldestination);

Sign in to comment.

Answers (1)

Adam Danz
Adam Danz on 28 Oct 2022
  1. creation_new_folder = mkdir(newfolder); --- "newfolder" is the path to your new folder. "creation_new_folder" is the status of the folder-creation (=1 means successful)
  2. When saving a specific figure, specify the figure handle as well as the path in the saveas()
fig = figure()
status = mkdir(newfolderPath);
saveas(fig, newfolderPath)
This is all explained in mkdir, saveas.


Find more on Visual Exploration in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by