Load concatenated .mat files in a for loop

조회 수: 1 (최근 30일)
Jose Rego Terol
Jose Rego Terol 2020년 1월 19일
댓글: woahs 2020년 1월 21일
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
woahs
woahs 2020년 1월 19일
The function dir usually returns the matches in alphanumerical order which means spike 11 comes after spike 1 (as would spike 10, spike 12, etc..) and before spike 2. Either rename your files such that they are ordered alphanumerically as you'd expect them to (e.g. spike 01, spike 02, ... spike 10, spike 11) or pad the filenames with 0's programatically and re-sort.
Stephen23
Stephen23 2020년 1월 20일
"The function dir usually returns the matches in alphanumerical order ..."
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:

댓글을 달려면 로그인하십시오.

채택된 답변

Jose Rego Terol
Jose Rego Terol 2020년 1월 19일
Hi Kevin,
"pad the filenames with 0's programatically and re-sort. "
I tried this option "Spike Nr_01, Spike Nr_02 ... Spike Nr_010" and the loop is stil taking the spike 11 before de second one.
Rename the files manually is just unacceptable. I have 60 spikes, so six times this error.
Can I change the order of function dir? I've checked the mathlab page and there is nothing about it.
  댓글 수: 6
Stephen23
Stephen23 2020년 1월 20일
편집: Stephen23 2020년 1월 20일
Calling natsortfiles on the full filenames is not required in this example as the folder is always exactly the same (i.e. myFolder). It would be simpler and more efficient to follow the examples in the natsortfiles documentation (i.e. get rid of cellfun and fileparts, and call fullfile inside the loop):
S = dir(fullfile(myFolder,'Spike No*.mat'));
C = natsortfiles({S.name});
for k = 1:numel(C)
fprintf('Now reading %s\n',C{k});
F = fullfile(myFolder,C{k});
% Call the function Spike_analysis
end
woahs
woahs 2020년 1월 21일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Just for fun에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by