How do i save a file into a folder in a different path?

2 views (last 30 days)
I am coding a project that uses neural networks. my neural networks are saved into the same path as this piece of code. i want to save each segmented image in another file location, each with a different name.
%Open files
testfiledir = 'X:\eee_biophotonics1\Shared\SPOT dataset (Training data)\Testing Images\123 images on 123 network\OCT';
matfiles = dir(fullfile(testfiledir, '*.tif'));
nfiles = length(matfiles); %finds length of file
data = cell(nfiles);
for i = 1 : nfiles
fid = fopen( fullfile(testfiledir, matfiles(i).name) ); %retrieves specific file
load('net_123.mat'); % This loads the network back into MATLAB
result = semanticseg(fid,net_123); % This segments an image "I" using the network
filename='X:\eee_biophotonics1\Shared\SPOT dataset (Training data)\Testing Images\123 images on 123 network\nn segmented';
save(result,'%d',i,'-tif');
fclose(fid);
end

Accepted Answer

Rachel Ball
Rachel Ball on 16 Dec 2022
%Open files for running through NN
fileNames_retrieve = dir(['dir_1\*.tif']);
foldername = ['dir_1'];
%retrieve directory for segmented images to be placed into
fileNames_save = ['dir_2'];
matfiles = length(fileNames_retrieve); %finds number of files in directory
for M = 1 : matfiles
file = fileNames_retrieve(M).name; %finds name of the pre-segmented image
im = fullfile(foldername,file);
image = imread(im); %selects specific image
result %add some process here
fullFileName = fullfile(fileNames_save, file); %creates full
%directory to save file
save(fullFileName, "result") %saves specific file
end

More Answers (1)

Dimitri MANKOV
Dimitri MANKOV on 1 Dec 2022
Edited: Dimitri MANKOV on 1 Dec 2022
Hi Rachel,
That should be fairly straightforward. You can create:
  • a new directory each iteration of the 'for' loop, and save your file there, and/or
  • a new file each iteration of the 'for' loop, and save it in a pre-defined directory.
Here is an example in which I generate a new variable 'a' every iteration, and save it in a new folder that I create in the current directory each time (using a different file name every time as well):
for idx = 1:10
a = rand(5);
mydirname = [cd,'\TMP',num2str(idx)];
myfilename = [mydirname,'\file',num2str(idx),'.mat'];
mkdir(mydirname)
save(myfilename,'a')
end
I hope this is helpful!
Dimitri

Categories

Find more on Image Processing and Computer Vision 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