How do I get a structure to include all the data that I want?
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Stephen23
le 3 Sep 2022
Modifié(e) : Stephen23
le 3 Sep 2022
"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
le 3 Sep 2022
@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".
Réponses (1)
Walter Roberson
le 2 Sep 2022
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 commentaires
Image Analyst
le 3 Sep 2022
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
le 3 Sep 2022
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()
Voir également
Catégories
En savoir plus sur Environment and Settings dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!