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

9 次查看(过去 30 天)
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.

采纳的回答

Bjorn Gustavsson
Bjorn Gustavsson 2022-8-5
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 个评论
Karuna Skipper
Karuna Skipper 2022-8-8
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 2022-8-8
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?).

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Adding custom doc 的更多信息

标签

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by