Info

This question is closed. Reopen it to edit or answer.

How to load two different variables, one after the other, in a nested for-loop with N number of variables for each type.

1 view (last 30 days)
Hi all,
I have two different files, "Spike No" and "Foot Spike". The number of each file is equal. Let's say 100 files for Spike No and 100 files for Foot Spike("Foot spike No1" to "Foot spike No 100", and "Spike No1" to "Foot Spike No 100"). Each file contains a vector (one column, N rows). However, the number of rows is different for each Foot spike compared to its cognate file (e.g. 245 rows for Foot Spike No1 and 14.000 rows for Spike No1).
I want to run a for-loop to load the first Foot Spike file ("Foot Spike No1"), get the length of this vector, load the cognate file ("Spike No1"), and shorten the length of the Spike file ("Spike No1") based on the length of the Foot spike vector. Then, load the next Foot Spike file ("Foot Spike No2") get the length of this vector, load the cognate file ("Spike No2"), and shorten the length of the Spike file ("Spike No2") based on the length of the Foot spike vector. This routine should run N times.
The problem came up when I tried to load the second file ("Spike No"). The code loads all these files instead of taking only one file.
I place a break at the end of the second loop, but the code always loads the first file of this loop.
myFolder=pwd
% Provides the file pattern of all spikes files in the current folder.
filePattern = fullfile(myFolder, 'Spike No*.mat'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
theFiles = cellfun(@(x, y) fullfile(x, y), {theFiles.folder}', {theFiles.name}', 'UniformOutput', false); % Sort the files in order for the for-loop
theFiles_natSort = natsortfiles(theFiles);
% Provides the file pattern of all foot signal files in the current folder.
filePattern_fs = fullfile(myFolder, 'Foot signal*.mat'); % Change to whatever pattern you need.
theFiles_fs = dir(filePattern_fs);
theFiles_fs = cellfun(@(x, y) fullfile(x, y), {theFiles_fs.folder}', {theFiles_fs.name}', 'UniformOutput', false); % Sort the files in order for the for-loop
theFiles_natSort_fs = natsortfiles(theFiles_fs);
for j = 1 : length(theFiles_natSort_fs)
fullFileName_fs = theFiles_natSort_fs{j};
[~, baseFileName_fs, ext] = fileparts(fullFileName_fs);
fprintf(1, 'Now reading %s\n', baseFileName_fs)
load(fullFileName_fs);
length(If);
length(Tf);
for k = 1 : length(theFiles_natSort)
fullFileName = theFiles_natSort{k};
[~, baseFileName, ext] = fileparts(fullFileName);
fprintf(1, 'Now reading %s\n', baseFileName)
load(fullFileName);
I(length(If)+1:end);
T(length(Tf)+1:end);
break
end
end
Now reading Foot signal No1
Now reading Spike No1
Now reading Foot signal No2
Now reading Spike No1
Now reading Foot signal No3
Now reading Spike No1
Now reading Foot signal No4
Now reading Spike No1
Now reading Foot signal No5
Now reading Spike No1
Now reading Foot signal No6
Now reading Spike No1
Thanks
  2 Comments
Stephen23
Stephen23 on 4 Feb 2020
Note that to write more efficient and more robust code you should always load into an output variable (which is a scalar structure):
S = load(...);
and then access its fields. Consider the latent fault in your code if one of the files does not contain a particular variable.
Jose Rego Terol
Jose Rego Terol on 4 Feb 2020
Thanks Stephen for your comment.
Somentimes Matlab is too abstract for me. I will implement the changes you suggest.

Answers (1)

Jose Rego Terol
Jose Rego Terol on 4 Feb 2020
for j = 1 : length(theFiles_natSort_fs)
fullFileName_fs = theFiles_natSort_fs{j};
[~, baseFileName_fs, ext] = fileparts(fullFileName_fs);
fprintf(1, 'Now reading %s\n', baseFileName_fs)
load(fullFileName_fs);
length(If);
length(Tf);
fullFileName = theFiles_natSort{j};
[~, baseFileName, ext] = fileparts(fullFileName);
fprintf(1, 'Now reading %s\n', baseFileName)
load(fullFileName);
I(length(If)+1:end);
T(length(Tf)+1:end);
end
No nested for-loop needed.
The solution was so easy.

Community Treasure Hunt

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

Start Hunting!