I want to loop for files in workspace

Dear all,
I have a lot of data that I imported into my workspace. Let's say I have data1, data2,... and dataN in my workspace. For example, I want to get length(data1), length(data2),... and length(dataN) automatically using a loop. Do I have any way to do that?

1 Comment

"Let's say I have data1, data2,... and dataN in my workspace."
And that is the start of your problems: badly-designed data with pseudo-indices forced into the variable names. Accessing numbered variable names in a loop is one way that beginners force themselves into writing slow, complex, inefficient, buggy code that is difficult to debug:
Much better data design would use one array, then you could use simple and efficient indexing, just like MATLAB is designed for.

Sign in to comment.

 Accepted Answer

"Let's say I have data1, data2,... and dataN in my workspace."
Lets assume that you did not name them all by hand, but instead that you LOADed them one-by-one from some MAT files. In which case, that is the correct place to fix the badly-designed data, e.g. renaming the variable in each MAT file to use exactly the same name, which would make it much easier to write simple, efficient, robust code to process your data.
Remember to always LOAD into an output variable!
If you cannot rename the MAT file vairiables and there is exactly one variable per MAT file, then we can LOAD them like this to avoid having lots of ugly numbered variable names in the workspace:
P = 'absolute or relative path to where the files are saved';
S = dir(fullfile(P,'*.mat'));
N = numel(S);
C = cell(1,N);
for k = 1:N
F = fullfile(P,S(k).name);
C(k) = struct2cell(load(F));
end
V = cellfun(@numel,C)
Simpler and much more efficient than trying to mess around with dynamic variable names.

7 Comments

Sir,
It helped me very well, but I wonder why it shows an error when I change the extension you used. For example, I may want a file with extension .txt or another . It works very well with the .mat extension.
Stephen23
Stephen23 on 12 May 2022
Edited: Stephen23 on 12 May 2022
"but I wonder why it shows an error when I change the extension you used."
Of course you cannot use code written to import MAT files to also import MP3 files or XLSX files or SVG files or any other file types. If you want to import another type of file, then you will need to select/obtain/write a function that imports that kind of file, and modify the code to suit e.g. remove STRUCT2CELL.
"For example, I may want a file with extension .txt or another"
Then you would probably select READMATRIX or DLMREAD or ... whatever, depending on the file format. Because you did not upload a sample file I cannot help you to select an approriate function.
"It works very well with the .mat extension."
Because the code I gave you is for MAT files.
Sir,
This is some part of the code I am writing. I included your code and it solved many problems. The problem is that I am not the one who will select the extension, but it will be selected by the user. The following code will only work if the user enters mat in the command window; otherwise it will not work.
%%
close all
clc
My_directory=input('Pleas enter the address of the directory containing strong motion records: ','s');
% Here you are supposed to put your folder name in command window
% Note that the input should look like C:\Users\Kumsa\Desktop\OCM_MAT(for
% my case)
cd(My_directory);
disp('You are now in the directory containing the strong motion records')
extension=input('Please enter the extension of the record files: ','s');
% Please put proper input, for example, txt or mat.
ext_code =['','*.',extension,''];
directory_all=strcat([My_directory,'\'], ext_code);
%%
S = dir(directory_all);
P = pwd;
N = numel(S);
C = cell(1,N);
for i=1:N
F=fullfile(P,S(i).name);
C(i) = struct2cell(load(F));
end
Do not use CD to access data files. Changing directories to access data files is slow and makes debugging harder. You can always use absolute/relative filenames, just like I showed you (using FULLFILE).
"The following code will only work if the user enters mat in the command window; otherwise it will not work."
It will not work at all, because you replaced the k index with N, which makes the loop do nothing.
"The problem is that I am not the one who will select the extension, but it will be selected by the user."
There is nothing stopping you from importing different file types:
P = input('Pleas enter the address of the directory containing strong motion records: ','s');
E = input('Please enter the extension of the record files: ','s');
S = dir(fullfile(P,sprintf('*.%s',E)));
N = numel(S);
C = cell(1,N);
for k = 1:N
F = fullfile(P,S(k).name);
switch lower(E)
case 'mat'
C(k) = struct2cell(load(F));
case 'xxx'
C{k} = importwhatever(F);
otherwise
C{k} = readmatrix(F);
end
end
Add more cases as required.
I got this minor error after running,
Pleas enter the address of the directory containing strong motion records: C:\Users\Kumsa\Desktop\OCM_MAT
Please enter the extension of the record files: txt
Unrecognized function or variable 'x'.
Error in Project (line 12)
switch lower(x)
@Chuchu Debebe: fixed now, try it again.
Thank you, sir. It worked!!

Sign in to comment.

More Answers (1)

s = whos ;
for i = 1:length(s)
s(i).size
end

Products

Release

R2022a

Community Treasure Hunt

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

Start Hunting!