필터 지우기
필터 지우기

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

조회 수: 13 (최근 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