How do I get a structure to include all the data that I want?
조회 수: 1 (최근 30일)
이전 댓글 표시
folder_list = dir([data_dir filesep currentfolder(3).name]);
this is my code. I want folder_list to grab everything from 3 to 7. if I use a simple for loop from 3:7, it just gives me the data for 7.
댓글 수: 5
Stephen23
2022년 9월 3일
편집: Stephen23
2022년 9월 3일
"I want folder_list to grab everything from 3 to 7."
That looks suspiciously like a buggy attempt to ignore the dot directory names:
As Walter Roberson wrote in that last link: "In short: if your code assumes that '.' and '..' are the first two entries in a directory, your code has a bug (even in MS Windows). If your code assumes that directory entries are returned in any sorted order, your code has a bug (in all OS.)"
Jan
2022년 9월 3일
@Khush Patel: Do not do this:
addpath(genpath(pwd));
Adding the folder, which contains data files, to Matlab's PATH has no advantage, but severe disadvantages. Matlab's PATH should include only the folders containing the M-files of the used functions. Use absolute path names for data files instead:
fid = importdata(fullfile(data_dir, folder_list(ifolder).name));
I do not see in your code the command matching the description "if I use a simple for loop from 3:7".
답변 (1개)
Walter Roberson
2022년 9월 2일
For example, to read all png files in all subfolders of data_dir, storing the input as a cell array of cell arrays broken up by folder.
dinfo = dir(data_dir);
dinfo(~[dinfo.isdir]) = []; %remove non-folders
dinfo(ismember({dinfo.name}, {'.', '..'})) = []; %remove . and ..
foldernames = fullfile({dinfo.folder}, {dinfo.name});
numfolders = length(foldernames);
folder_images = cell(numfolders, 1);
for D = 1 : numfolders
thisfolder = foldernames{D};
finfo = dir(thisfolder, '*.png');
filenames = fullfile({finfo.folder}, {finfo.name});
numfiles = length(filenames);
these_images = cell(numfiles, 1);
for F = 1 : numfiles
thisfile = filenames{F};
these_images{F} = imread(thisfile);
end
folder_images = these_images;
end
댓글 수: 5
Image Analyst
2022년 9월 3일
Why 3-7? Are you trying to avoid . and .. which is what this does:
currentfolder(~[currentfolder.isdir]) = []; %remove non-folders
or are your first and second folder not . and ..?
Walter Roberson
2022년 9월 3일
No, I cannot do that. Your current code is incorrect in design.
You should never rely on the relative position of files within a folder. None of the file systems you are likely to ever encounter impose any internal ordering on names, and none of the operating systems supported by MATLAB define a fixed order to return names in response to queries of directory information.
The NTFS file system commonly used by Windows has some "typical" behaviour, but the typical behaviour is not strictly adhered to, and is different from what most people expect... and depends in part on the region setting of the user who created the file system. So copying between drives can result in different ordering of files.
You should always code in terms of accepting or rejecting files based upon names or other attributes, not on the basis of order of files returned by dir()
참고 항목
카테고리
Help Center 및 File Exchange에서 Environment and Settings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!