Append to original file name and and save new file to directory?

8 vues (au cours des 30 derniers jours)
Karuna Skipper
Karuna Skipper le 5 Août 2022
Commenté : Bjorn Gustavsson le 8 Août 2022
Concerning the following code:
D = 'C:\Users\[...]folder\';
S = dir(fullfile(D,'*.tif'));
for k = 1:numel(S)
OGFile = imread(fullfile(D,S(k).name));
imshow(OGFile);
[centers,radii] = imfindcircles(OGFile,[5 15], 'Sensitivity',0.85, 'Method', 'TwoStage', 'EdgeThreshold',0.20);
h = viscircles(centers, radii,'Color','c', 'LineWidth',1.5, 'EnhanceVisibility',false);
F = getframe;
%save as originalfilename_circles.tif
end
I currently load all files in "folder" and perform the imfindcircles and viscircles functions upon them. I use getframe to capture the viscircles image, and would like to know how I can save this as [original file name]_circles? As in, append the string "_circles" to the end, and save to folder D.
I know the latter part includes:
imwrite(F.cdata,[filename]);
but am not sure how to specify directory and new file name there.
Thank you so much for any help :) Please let me know if there's any better way I could execute the above code, and forgive any mistakes here! I am very new to matlab.

Réponse acceptée

Bjorn Gustavsson
Bjorn Gustavsson le 5 Août 2022
You should be able to separate the path, name and ext of the full filename using fileparts. That should make it reasonably straightforward to append your "_circles" to the filename. Perhaps a modification something like:
D = 'C:\Users\[...]folder\';
S = dir(fullfile(D,'*.tif'));
for k = 1:numel(S)
fFname = fullfile(D,S(k).name)
OGFile = imread(fFname);
imshow(OGFile);
[centers,radii] = imfindcircles(OGFile,[5 15], 'Sensitivity',0.85, 'Method', 'TwoStage', 'EdgeThreshold',0.20);
h = viscircles(centers, radii,'Color','c', 'LineWidth',1.5, 'EnhanceVisibility',false);
F = getframe;
[fF_path,fFname,fFext] = fileparts(fFname);
fFnameExt = fullfile(fF_path,[fFname,'_circles',fFext]);
% Maybe you should consider to save to a results-directory and not
% fill one directory with both original data and analysis results
%save as originalfilename_circles.tif
end
HTH
  2 commentaires
Karuna Skipper
Karuna Skipper le 8 Août 2022
Thank you so much!
Yes, I plan to saving to a second directory - I have much more analysis to add to this program, it would definitely be too much to have it all in a single folder.
I would do that as something akin to, this, right?
SavePath = 'C:\[path]\';
fFnameExt = fullfile(fF_path,[SavePath,'_circles',fFext]);
Bjorn Gustavsson
Bjorn Gustavsson le 8 Août 2022
Good that it helped.
Something like that ought to be OK, but I think it should look like:
[fF_path,fFname,fFext] = fileparts(fFname);
SavePath = 'C:\[path]\';
fFnameExt = fullfile(SavePath,[fFname,'_circles',fFext]);
You might try to build the SavePath-variable with fullfile too - to extract the relevant parts from fF_path if you need that and possibly to make transport of your scripts to UNIX-like OSes easier - but then I don't know how to handle the MS "C:"-drive-designation, so that might be tricky (or trivial?).

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Adding custom doc dans Help Center et File Exchange

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by