How to add the same headers to all matrices in a loop
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Max Moester
am 2 Mär. 2022
Kommentiert: Max Moester
am 3 Mär. 2022
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 Kommentar
Stephen23
am 2 Mär. 2022
Bearbeitet: Stephen23
am 2 Mär. 2022
"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.
Akzeptierte Antwort
Stephen23
am 2 Mär. 2022
Bearbeitet: Stephen23
am 2 Mär. 2022
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 Kommentare
Stephen23
am 3 Mär. 2022
Bearbeitet: Stephen23
am 3 Mär. 2022
"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.
Weitere Antworten (1)
David Hill
am 2 Mär. 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 Kommentar
Siehe auch
Kategorien
Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!