Append to original file name and and save new file to directory?
7 views (last 30 days)
Show older comments
Karuna Skipper
on 5 Aug 2022
Commented: Bjorn Gustavsson
on 8 Aug 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.
0 Comments
Accepted Answer
Bjorn Gustavsson
on 5 Aug 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 Comments
Bjorn Gustavsson
on 8 Aug 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?).
More Answers (0)
See Also
Categories
Find more on Adding custom doc 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!