Import multiple .text matrices in array

1 次查看(过去 30 天)
I have a set of multiple (hundreds) matrix in .text format, and I need to import them in MATLAB for different kind of analysis.
Because the steps I need to take are the same foreach matrix, I would like to save all of them in an array, in order to use for loops in a function.
At the moment I'm only able to import each of them in the workspace individually doing:
files = dir('*.txt');
for i=1:length(files)
eval(['load ' files(i).name ' -ascii']);
end
In this way I only obtain hundreds of matrices with different names in the workspace, which is very bad to work with.
What is the best way to import the matrices and directly put them inside an array?

采纳的回答

Jakob B. Nielsen
Jakob B. Nielsen 2019-12-4
编辑:Jakob B. Nielsen 2019-12-5
Do you have experience working with structures? In your workspace there will be only one item, which makes keeping the overview much easier.
for i=1:length(files)
YourStructure(i).matrix=load(files(i).name,'-ascii');
YourStructure(i).name=files(i).name
end
YourStructure(3).matrix %<-- you call it this way, where the indexing is now in the structure. This particular case it will call the 3rd entry in the structure.
  2 个评论
IGArg
IGArg 2019-12-4
编辑:IGArg 2019-12-4
Actually trying to implement this solution I get an error. Apparently it's not possible to associates to a variable (or the entry of a structure) the value returned by eval.
It's like eval can only generate a variable but not directly associate it to another, so I still have the problem of how to actually fill the structure (just as I would have for arrays).
UPDATE: Using the load function alone without eval works fine in your solution, thanks for your reply!
Stephen23
Stephen23 2019-12-4
编辑:Stephen23 2019-12-4
Simpler, neater, less buggy, and easier to debug without eval:
YourStructure(i).matrix = load(files(i).name,'-ascii');
eval should be avoided for such trivial code:

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Cell Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by