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 commentaires

dpb
dpb le 23 Avr 2019
Attach a piece of a file so folks have a chance to do something with it...
"Trying to learn the syntax is just not realistic for me given that we aren't using this software anymore."
What does the above refer to by "this"--Matlab, maybe?
As far as the complaint of realistic, if your employer expects to retrieve the data, they'll have to have the expectation of expending the resources needed to do so; it's not somebody else's responsibility to do so for them.
They can either pay you to learn enough to do the job or hire outside expertise...
If you did post the data, it's possible a volunteer here could/would look at it, but while the purpose of the forum is to support Matlab users use questions, it's not really the purpose to be a free consulting service.
Guillaume
Guillaume le 23 Avr 2019
To add to what dpb said (treating us as a consulting service is very off-putting personally, we're here to help people learn matlab):
or reformat it better (which I've seen suggested a few times in my research so far).
I don't see how exporting that data to excel or csv is in any way better than what you have. I'd consider it much worse. If you wanted to export it to a database or similar, I'd understand but excel?
dpb
dpb le 23 Avr 2019
As far as doing the original operation, looks like you've read or the .mat file was a table from the format of the posted text; it appears a grouping variable by uniqueid would solve your problem almost trivially.
Peter Erhardt
Peter Erhardt le 23 Avr 2019
Since I am getting pushback about this being "free consulting", I will delete this question.
My point was that I will be using MATLAB once to export this data to a flatfile to be analyzed elsewhere and then we won't be using it again. So the hope was someone could sketch out the code needed and I could tweak it rather than needing to read up on structures, tables, functions, etc. needed for a one time piece of code.
The "better format" point was while researching similar exporting questions from other users, some suggestions were to go back to the original data source and format/store the data differently prior to it getting into a .MAT file. My point there is that is not an option for me, it is only available in this format.
dpb
dpb le 23 Avr 2019
Modifié(e) : dpb le 23 Avr 2019
Well, I wouldn't go that far, yet, anyways...but it is a little out of the direct purpose of the forum given no intention of using ML in future -- reminds me a lot of the "DoIt4Me!" homework questions with no visible effort from the student.
Attach .mat file; somebody at least could double-check just what form it is in and, as noted, it might be trivial enough...
I think my suggestion above could be implemented pretty easily even with minimal amount of study.
Where's the dood/doodette that wrote the code that created the .mat files? Why not bring him/her back?
Walter Roberson
Walter Roberson le 23 Avr 2019
I am not clear as to whether the .mat files store table() objects, or store struct() objects, or have a collection of variables.
dpb
dpb le 23 Avr 2019
Ayup, that's why we need to see the .mat file to know what really gots...it might be pretty trivial, might not be, but how to know without knowing? :)
Peter Erhardt
Peter Erhardt le 23 Avr 2019
I'm working on getting a few rows out and de-identifying it.
Peter Erhardt
Peter Erhardt le 23 Avr 2019
Ok attached is the first 4 rows.
My hope is to end up with 1 file with 1045 rows + 1208 rows + 1365 rows + 1220 rows.
'pid' and 'subjind' would be repeated for their respective rows.
'header' I don't need.
'rtime' through 'subphase' would have the underlying double values split out.

Connectez-vous pour commenter.

 Réponse acceptée

Guillaume
Guillaume le 23 Avr 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 commentaire

dpb
dpb le 23 Avr 2019
Modifié(e) : dpb le 24 Avr 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...

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Data Type Identification dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by