Load concatenated .mat files in a for loop
1 view (last 30 days)
Show older comments
Jose Rego Terol
on 19 Jan 2020
Commented: woahs
on 21 Jan 2020
Hello all.
I have this code. The problem is that, when I have 11 files, the for loop takes the file "spike 11.mat" before the file "spike 2.mat". In other word, the first file loaded is "spike 1.mat", then "spike 11.mat", and finally "spike 2.mat".
How can I tell the loop to take the first file, the second file, and so on?
Here is the for loop:
clear all, clc
% Specify the folder where the files live.
myFolder = 'C:\Users\usuario\Desktop\Lab\Amp_scripts\Spikes not analyzed\B2310\B2310\Exported_ROI_Second third_B2310.mat';
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, 'Spike No*.mat'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', baseFileName)
%Call the function Spike_analysis
2 Comments
Stephen23
on 20 Jan 2020
In fact no OS the MATLAB currently operates on takes into account the numeric values when returning the folder contents to DIR. On Windows the names are usually sorted into character (aka "asciibetical") order. Read these to know more:
Accepted Answer
Jose Rego Terol
on 19 Jan 2020
6 Comments
woahs
on 21 Jan 2020
Agreed. I just tend to protect for different fullpaths but in this case, I don't see it being a problem to omit the file path unless you need to sort a set of filenames stored in different folders.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!