How to add the same headers to all matrices in a loop
Show older comments
I'm working on a small script to extract all the data points from a map with multiple .mat files. This is already done as seen in the picture below however, I would like to add headers to all the matrices. For now, I am only capable to execute it for one matrix but I want this result for all matrices (e.g. with a loop) Complete script.


Many thanks!
Max
1 Comment
"Desired result"
Ugh, do NOT store perfectly good numeric data in a cell array like that. Unless of course you really want to make processing your numeric data slow and complex.
Much better: use a table. Or a basic numeric array.
"eval"
Ugh. do NOT dynamically name variables like that. Unless of course you really want to make processing your numeric data slow, complex, inefficient, buggy, and difficult to debug.
Much better: use arrays and indexing, just like MATLAB is designed for.
Accepted Answer
More Answers (1)
David Hill
on 2 Mar 2022
files=dir('*.mat');
m=[];
for i=1:length(files)
t=struct2cell(load(files(i).name));
m=[m;table2array(t{1})];
end
T=array2table(m);
T.Properties.VariableNames={'x','y','z'};
1 Comment
Categories
Find more on Multidimensional Arrays 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!