필터 지우기
필터 지우기

How do I get a structure to include all the data that I want?

조회 수: 1 (최근 30일)
Khush Patel
Khush Patel 2022년 9월 2일
댓글: Jan 2022년 9월 3일
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
Stephen23 2022년 9월 3일
편집: Stephen23 2022년 9월 3일
Jan
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
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
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
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 CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by