How to export a struct to flat file?
11 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Peter Erhardt
am 23 Apr. 2019
Bearbeitet: dpb
am 24 Apr. 2019
First off, I have no background in MATLAB at all. I was given some .MAT files at work that I need to open and export to any flat file to be imported into another program. This is the only source of data, no opportunity to get it elsewhere or reformat it better (which I've seen suggested a few times in my research so far).
Here are a few example rows of data I have from 1 file. There are more columns of data, but the formatting got messed up. This file has 1500+ rows in it.
Each file has a slightly different set of columns, but pretty similar.
uniqueid id header var1 var2 var3 var4
'Unique ID 1' 1 9x1 cell 1045x1 double 1045x1 double 1045x1 double 1045x1 double
'Unique ID 2' 2 9x1 cell 1208x1 double 1208x1 double 1208x1 double 1208x1 double
'Unique ID 3' 3 9x1 cell 1365x1 double 1365x1 double 1365x1 double 1365x1 double
I'd like to get this into excel/csv/txt in 1 worksheet, with the 1045 rows for Unique ID 1, followed by the 1208 rows for Unique ID 2, followed by the 1365 rows for Unique ID 3, etc. Unique ID 1 and ID would just be repeated. Header I dont really need (they're column heading for the other variables?) var1, 2, 3, etc is what I really need parsed out.
I'd appreciate any code to get this exported to csv, txt, excel. Trying to learn the syntax is just not realistic for me given that we aren't using this software anymore.
I'm messing around with struct2table, xlswrite, writetable but really don't know what I'm doing so please be specific in any response.
9 Kommentare
Akzeptierte Antwort
Guillaume
am 23 Apr. 2019
fnames = fieldnames(MCI1);
c = struct2cell(MCI1)';
heights = cellfun(@numel, c(:, 4));
t = [cell2table(repelem(c(:, [1 2]), heights, 1), 'VariableNames', fnames(1:2)), ...
array2table(cell2mat(c(:, 4:end)), 'VariableNames', fnames(4:end))];
writetable(t, 'somefile.xlsx');
1 Kommentar
dpb
am 23 Apr. 2019
Bearbeitet: dpb
am 24 Apr. 2019
Precisely, or variations thereof...yours keeping the table is good; I'd written another for the numeric values alone along the lines of
tM=struct2table(MCI1);
A=table2array(tM(:,4:end));
L=cellfun(@length,A(:,1));
A=cell2mat([arrayfun(@(i,L)kron(i,ones(L,1)),[1:size(A,1)].',L,'uni',0)]);
Agree, however, this whole idea seems a large step backwards rather than forward...
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Data Type Identification 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!