Change variable name in loop to store matrices

조회 수: 1 (최근 30일)
Adriel Perez Tellez
Adriel Perez Tellez 2018년 8월 6일
댓글: Stephen23 2018년 8월 6일
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
Stephen23
Stephen23 2018년 8월 6일
편집: Stephen23 2018년 8월 6일
"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.
Adriel Perez Tellez
Adriel Perez Tellez 2018년 8월 6일
Great points!

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

채택된 답변

Stephen23
Stephen23 2018년 8월 6일
편집: Stephen23 2018년 8월 6일
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:
  댓글 수: 2
Adriel Perez Tellez
Adriel Perez Tellez 2018년 8월 6일
편집: Stephen23 2018년 8월 6일
This works perfectly! Thanks!
Stephen23
Stephen23 2018년 8월 6일
@Adriel Perez Tellez: I hope that it helps!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by