How to add the same headers to all matrices in a loop
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
Stephen23
2022-3-2
编辑:Stephen23
2022-3-2
"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.
采纳的回答
Stephen23
2022-3-2
编辑:Stephen23
2022-3-2
If you really want to use MATLAB efficiently and make processing your data easier then:
- use indexing rather than dynamically naming variables (do not shoot yourself in the foot like that). Once you magically name variables then you force yourself into writing slow, complex, inefficient code which is buggy and hard to debug, just trying to perform the basic task of trying to access your data. Ugh. No.
- use a table rather than splitting up perfectly good numeric data into a cell array (what a waste of MATLAB).
Something like this would be a much much much better approach than what you are trying:
P = 'absolute or relative path to where the files are saved';
S = dir(fullfile(P,'*.mat'));
for k = 1:numel(S)
F = fullfile(P,S(k).name);
T = load(F);
S(k).data = array2table(T.points,'VariableNames',{'X','Y','Z'});
end
You can trivially and efficiently access the data using basic indexing, for example the second file:
S(2).name % filename
S(2).data % table of imported data
3 个评论
Stephen23
2022-3-3
编辑:Stephen23
2022-3-3
"I don't want to access the data, I want an automated process that adds the headers to the data files. Next, I want to export all these datasets to csv files so that I can open them in excel including the correct headers."
Then using a table is most likely the easiest approach, based on WRITETABLE. If you don't need to process the data further within MATLAB then you don't need to store them in S. For example:
P = 'absolute or relative path to where the files are saved';
S = dir(fullfile(P,'*.mat'));
for k = 1:numel(S)
% Load MAT file data (numeric array):
F = fullfile(P,S(k).name);
T = load(F);
% Convert array to table with header:
T = array2table(T.points, 'VariableNames',{'X','Y','Z'});
% Save table as CSV file with header:
[~,G,~] = fileparts(S(k).name);
G = fullfile(P,sprintf('%s.csv',G));
writetable(T,G)
end
Trying to achieve that with dynamically-named variables will be much more complex and inefficient.
更多回答(1 个)
David Hill
2022-3-2
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrices and Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!