how to read sequence of files that are seemingly random

I am trying to read 1000+ files with the following filename format: myPos_HH_MM_SS_SSS.mat. There seems to be no pattern for HH_MM_SS_SSS. How do I perform this?

Antworten (2)

dpb
dpb am 20 Jul. 2018
Bearbeitet: dpb am 20 Jul. 2018
d=dir(fullfile('YourBaseDirectory','myPos_*.mat');
for i=1:length(d)
load d((i).name
...
% Do what ever needs done with variables in the .mat file here
...
end
will process in alphabetical order. If need in chronological order then
d=dir(fullfile('YourBaseDirectory','myPos_*.mat');
dn=[dn(:).datenum].'; % get the datenums for each file
[~,ix]=sort(dn); % and sort ascending, save order index
d=d(ix); % rearrange the directory struct order
From here on, everything's the same...
for i=1:length(d)
load d((i).name

4 Kommentare

cgo
cgo am 20 Jul. 2018
Bearbeitet: Image Analyst am 20 Jul. 2018
sorry I am so confused by this. So I do not need any further sorting, just reading my files, doing something to them and saving them in another way.
I am confused by the order you presented.
dpb
dpb am 20 Jul. 2018
Bearbeitet: dpb am 20 Jul. 2018
Well, it wasn't clear whether the "seemingly random" phrase was a plea for order or only one for just how to access if didn't necessarily know the exact name for every one...Free extra service, if you will! :)
Unfortunately, when I copied the initial code to make the second example if order were to be important, I didn't realize that I had elided it from the first loop...that did leave some confusion, granted.
cgo, that's what I did. Did you see my answer below? No sorting, just going through your mat files one-by-one just as dir() gives them to you.
dpb
dpb am 20 Jul. 2018
Which I re-edited to put back what I inadvertently deleted... :)

Melden Sie sich an, um zu kommentieren.

Image Analyst
Image Analyst am 20 Jul. 2018
You did not ask for any sorting (not sure why others are sorting). You simply asked how to read files that have random names, not a sequence in any particular alphanumeric order. So that means you can simply use dir('*.mat') to read the files, as per the FAQ: https://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F
% Specify the folder where the files live.
myFolder = pwd; % or wherever;
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isdir(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.mat'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
numberOfFiles = length(theFiles);
fprintf('Found %d .mat files in folder: "%s".\n', numberOfFiles, myFolder);
for k = 1 : numberOfFiles
baseFileName = theFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now loading %s\n', fullFileName);
% Now "read" (load) the mat file. Returned value will be a structure.
s = load(fullFileName)
% Now do whatever you want with s.
end

Kategorien

Tags

Gefragt:

am 19 Jul. 2018

Bearbeitet:

dpb
am 20 Jul. 2018

Community Treasure Hunt

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

Start Hunting!

Translated by