replacing uiget with pathname and filename

4 visualizaciones (últimos 30 días)
Golnoush Asaeikheybari
Golnoush Asaeikheybari el 17 de Mayo de 2019
Comentada: Golnoush Asaeikheybari el 17 de Mayo de 2019
I have the code below which loops over the subfolders in a folder oipens each and read a .raw image in each file. The problem is that this code is using uigetfile which makes me to choose and open a subflder and subsequently an image in the subfolder. I would not like to do all these manually because I have 500 subfolders. So, I commented the line which has uiget and instead I added the one line and three lines before that. They also give me the same filename and pathname that I get from uiget but the problem is that I do not know why I get the below error although filename and pathname are the same as the output of uiget. I would be very thankful if you could help.
function filenames = getfn(mydir, pattern)
%GETFN Get filenames in directory and subdirectories.
%
% FILENAMES = GETFN(MYDIR, PATTERN)
%
% Example: Get all files that end with 'txt' in the current directory and
% all subdirectories
%
% fn = getfn(pwd, 'txt$')
%
% Thorsten.Hansen@psychol.uni-giessen.de 2016-07-06
if nargin == 0;
mydir = pwd;
end
% computes common variable FILENAMES: get all files in MYDIR and
% recursively traverses subdirectories to get all files in these
% subdirectories:
getfnrec(mydir);
% if PATTERN is given, select only those files that match the PATTERN:
if nargin > 1
idx = ~cellfun(@isempty, regexp(filenames, pattern));
filenames = filenames(idx);
end
function getfnrec(mydir);
% nested function, works on common variable FILENAMES
% recursively traverses subdirectories and returns filenames
% with path relative to the top level directory
d = dir(mydir);
sub=cell(3,1);
sub{1}='patient';
sub{2}='000';
sub{3}=int2str(1);
sub{4}='_2CH_ED';
sub{5}='_gt';
sub{6}='.raw';
fname=[sub{1} sub{2} sub{3}];
filenames = {d(~[d.isdir]).name};
filenames = strcat(mydir, filesep, filenames);
dirnames = {d([d.isdir]).name};
dirnames = setdiff(dirnames, {'.', '..'});
for i = 1:numel(dirnames);
fulldirname = [mydir filesep dirnames{i}];
pathname = [mydir filesep dirnames{i}]; %I use this instead of pathname in uiget
filenames = [filenames, getfn(fulldirname)];
filename='mm.raw'; %I use this intead of filename in uiget
[filename, pathname] = uigetfile({'*.raw'},'Pick an Image');
fid=fopen([pathname filename],'r');
Data=fread(fid,inf,'uint8=>uint8');
Data1=Data;
fclose(fid)
% do a self correlation to find length of lines
fftData = fft(Data);
selfcorr = ifft(fftData .*conj(fftData ));
selfcorr = selfcorr -mean(selfcorr);
% find first peak in self correlation as indicator for length of line
peaks = selfcorr([2:end 1]) - selfcorr;
peaks = peaks([2:end 1]) - peaks;
ispeak = peaks < peaks([2:end 1]) & peaks < peaks([end 1:end-1]) & peaks < peaks(end)/10;
% length of line is considered width
k = numel(Data);
w = find(ispeak,1);
h = k / w;
IMG = reshape(Data,[w h]);
w
h
imshow(IMG);
%imresize(IMG, [1038,630]);
donut=double(IMG)
row=w; col=h;
...
end
end % nested function
end
error:
Error using fread
Invalid file identifier. Use fopen to generate a valid file identifier.
Error in getfn/getfnrec (line 57)
Data=fread(fid,inf,'uint8=>uint8');
Error in getfn (line 18)
getfnrec(mydir);

Respuesta aceptada

Stephen23
Stephen23 el 17 de Mayo de 2019
Editada: Stephen23 el 17 de Mayo de 2019
You use dir without any specific name+wildcards, so it will return all contents of that folder (i.e. files and folders). You then separate the filenames and folder names, and loop over the folder names, recursively calling the main function. That concept is okay, but you basically ignore the filenames (apart from collecting them into a cell array). You simply need to loop over those filenames!
Note that the code comments incorrectly describe the nested function that it is somehow recursive: in fact the recursion occurs by calling the main function, not the nested function. The nested function is called only once by the main function: this means that nested function adds complexity but basically does nothing useful whatsoever.
Actaully I think it would be simpler to call dir twice: once for the subfolders, and once for the files using a pattern to match exactly the file extension that you need. That will make your code simpler as you would be able to trivially loop over the matches folder/files names without requiring anything extra (your current approach would require splitting valid filenames from the list of matched folder contents).
Do NOT concatenate file paths / names. Use fullfile instead, e.g.:
fid = fopen(fullfile(pathname,filename),'r');
EDIT : I found the source of this very badly written code. You can find plently of much better recursive file-reading functions on FEX, I suggest that you look there instead:

Más respuestas (0)

Categorías

Más información sobre File Operations en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by