Filter löschen
Filter löschen

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

8 Ansichten (letzte 30 Tage)
Rachel Ball
Rachel Ball am 1 Dez. 2022
Beantwortet: Rachel Ball am 16 Dez. 2022
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';

Akzeptierte Antwort

Rachel Ball
Rachel Ball am 16 Dez. 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

Weitere Antworten (1)

Dimitri MANKOV
Dimitri MANKOV am 1 Dez. 2022
Bearbeitet: Dimitri MANKOV am 1 Dez. 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'];
I hope this is helpful!


Mehr zu Text Analytics Toolbox finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by