How can I iterate through an array using a for loop?
40 views (last 30 days)
Show older comments
Abigail
on 30 Jul 2024 at 13:51
Edited: Walter Roberson
on 30 Jul 2024 at 17:13
I want to iterate through an array of file locations using a for loop.
My current code is something like this:
% Paths where the files are located
P1 = C:\Users\me\Documents\\My Info
%Find excel files in path
%Pull data
% Write into a file
Now this code works great, but now I need to do the same thing to multiple paths while maintaining efficiency. To do this, I created an array of the paths I need to iterate through. How do I use a for loop to iterate through these paths? Here is what I have so far, but my code breaks when it tries to read the path because the text is not scalar when trying to use 'dir'. Here is what I currently have:
% Paths where the files are located
P1 = C:\Users\me\Documents\\My Info
P2 = C:\Users\me\Documents\\My data
%Array of paths
Array = {'P1, P2'}
for i = 1:length(Array)
%Find excel files in path
%Pull data
% Write into a file
0 Comments
Accepted Answer
Image Analyst
on 30 Jul 2024 at 14:13
The paths need to be enclosed in single or double quotes. Then when you put them into a cell array don't use quotes there. And you need to use fullfile to construct the full path. And don't use i (the imaginary constant) as a loop iterator. Untested code:
% Paths where the files are located
P1 = 'C:\Users\me\Documents\My Info';
P2 = 'C:\Users\me\Documents\My data';
%Array of paths
Array = {P1, P2};
for k = 1:length(Array)
thisFolder = Array{k};
% Find all Excel files in this folder.
filePattern = fullfile(thisFolder, '*.xls*');
fileList = dir(filePattern)
for f = 1 : numel(fileList)
fullFileName = fullfile(thisFolder, fileList(f).name);
fprintf('Processing %s.\n', fullFileName);
% Pull data
data = readmatrix(fullFileName);
% Write into am output file
baseFileName = sprintf('New %s', fileList(f).name)
outputFileName = fullfile(thisFolder, baseFileName);
writeMatrix(data, outputFileName);
end
end
Also, please read the FAQ: What is a cell array
for a good explanation of how to use cell arrays and when to use braces, parentheses, and brackets.
To learn other fundamental concepts, invest 2 hours of your time here:
0 Comments
More Answers (1)
Stephen23
on 30 Jul 2024 at 14:00
Edited: Stephen23
on 30 Jul 2024 at 14:14
Given an array of paths:
C = {'C:\Users\me\Documents\My Info','C:\Users\me\Documents\My data'};
F = @(p)dir(fullfile(p,'*.xlsx'));
D = cellfun(F,C,'uni',0); % or use a FOR-loop
S = vertcat(D{:}); % comma-separated list
Or assuming a small fixed number of paths:
S = [...
dir('C:\Users\me\Documents\My Info\*.xlsx');...
dir('C:\Users\me\Documents\My data\*.xlsx')];
Then loop over all files (including their paths!):
for k = 1:numel(S)
F = fullfile(S(k).folder,S(k).name);
...etc
end
Another option might be to use a datastore:
1 Comment
Sergio E. Obando
on 30 Jul 2024 at 14:20
+1 to using datastore. You should not need to use a for loop if you leverage read/readall and similar to write to file.
See Also
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!