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

2 ビュー (過去 30 日間)
Rachel Ball
Rachel Ball 2022 年 12 月 1 日
回答済み: Rachel Ball 2022 年 12 月 16 日
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

採用された回答

Rachel Ball
Rachel Ball 2022 年 12 月 16 日
%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

その他の回答 (1 件)

Dimitri MANKOV
Dimitri MANKOV 2022 年 12 月 1 日
編集済み: Dimitri MANKOV 2022 年 12 月 1 日
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

カテゴリ

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