Change variable name in loop to store matrices

2 views (last 30 days)
Hello everyone!
I have a bunch of txt files with the same format that I want to store as numeric arrays (matrices). I want to store these matrices in variables with name of the respective txt file. So far I have used the Matlab GUI "Import" to generate a function that import one file. My problem is that I have hundreds of these txt files, therefore I want to make this import in a loop. However, I have problems creating the variable to store matrices inside the loop.
Here is short extract of the things I have been trying:
file = dir('myFolder\*.txt'); %my files
caseName = {file.name}; % caseName{1} for example yields >> results_case0001.txt
for i = 1:length(caseName)
[~ , filename, ~] = fileparts(caseName{i});
filename = importfile(caseName{i}); % importfile is the function generated with GUI Import tool
end
I thought that since
filename
changes for every iteration, that it could be used as a variable name, but it does not work. I would appreciate any help! Thanks in advance!
Best regards, Adriel
  2 Comments
Stephen23
Stephen23 on 6 Aug 2018
Edited: Stephen23 on 6 Aug 2018
"I thought that since filename changes for every iteration, that it could be used as a variable name, but it does not work."
It could work in some limited circumstances, but it would be slow, complex, liable to bugs, and hard to debug.
Consider that there are many filenames which are not valid variable names: what would you expect your code to do if the filename was 123+99.txt, which is a perfectly valid filename. Any ideas? If you started to change the invalid names then you would have to consider the possibility of clashes, e.g. how would you change these two names: 1+2.txt and 1-2.txt? So you would need some way to rename them into unique names... at which point you have lost any connection between the filenames and the variable names.
You can see that not only is this more complex than you originally thought, but that it is not a very robust way to design or write code: it is fragile to changes in the filenames and what character the user puts in the names. It is always a bad idea to make the internal working of your code depend on some meta-data like filenames: you can store the filenames as data, but do not make your code use them in its own variable names.
The simpler alternative is to use indexing. Not only is indexing much more efficient, it will work exactly the same regardless of the filenames: using indexing is totally robust to all possible filenames on any possible OS, without requiring any magical tricks or slow, buggy code.

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 6 Aug 2018
Edited: Stephen23 on 6 Aug 2018
It is much simpler to just use indexing:
S = dir('myFolder\*.txt');
N = numel(S);
C = cell(1,N);
for kk = 1:N
C{kk} = importfile(S(kk).name);
end
This is just like the documentation shows:
What you are trying to do, magically naming variables after the filenames, is not recommended. It will make your code slow, complex, buggy, and hard to debug. Read this to know why:

More Answers (0)

Community Treasure Hunt

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

Start Hunting!