Getting cell array of structs to a usable form in R

20 views (last 30 days)
Hey there,
I have a lot of data manipulated in matlab which I have to transfer to R for data investigation. My data in matlab is of the following form:
>> myData
myData =
1×11 cell array
Columns 1 through 2
{1×1 struct} {1×1 struct}
Columns 3 through 4
{1×1 struct} {1×1 struct}
Columns 5 through 6
{1×1 struct} {1×1 struct}
Columns 7 through 8
{1×1 struct} {1×1 struct}
Columns 9 through 10
{1×1 struct} {1×1 struct}
Column 11
{1×1 struct}
If we look into one elelment:
>> myData{1}
ans =
struct with fields:
dcdcLineVoltage: [19000×1 double]
CurrentGridSide: [19000×1 double]
calculatedResistance: [19000×1 double]
rotSpeedAtRearMotorShaft: [19000×1 double]
time: [19000×1 double]
timestamp: [19000×1 int64]
synchronizedTime: [19000×1 double]
calculatedVelocity: [19000×1 double]
calculatedAcceleration: [19000×1 double]
dist: [19000×1 double]
converterCurrentBusSide: [19000×1 double]
converterVoltageBusSide: [19000×1 double]
tracInverterCurrent: [19000×1 double]
tracInverterVoltage: [19000×1 double]
batteryVoltage: [19000×1 double]
currentToBattery: [19000×1 double]
powerDemand: [19000×1 double]
directionFlag: [19000×1 logical]
adjustedDist: [19000×1 double]
smoothedDcdcLineVoltage: [19000×1 double]
I need to access each struct in R, does someone have a nice function to use to extract the data maybe as a csv? Do you have another idea?
The best I got so far was to combine all of the data to one long struct separated by NaN's and than unfold it in R... No beauty...
Thank you for your time!
  2 Comments
Guillaume
Guillaume on 27 Nov 2019
If all the structures are more or less the same format, converting them to tables would make sense. It's trivial to write tables to csv files.
However, since the structures are stored as scalars in a cell array, presumably there's differences in field names or other properties. (Otherwise, they would be stored as a structure array).
Assuming, they're all writable as csv, what would the files be called?
OneStone
OneStone on 27 Nov 2019
Thank you for your insight!
The reason why initialy they were stored in a cell array and not in a structure array I cannot tell. I think a struct array would have done the work too. Each struct corresponds to a certain drive, so the individual structs in the cell array (afterwords tables) should have names 'drive1', 'drive2', ...
Let me know if you need more detail

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 27 Nov 2019
destination_folder = 'C:\Somewhere\somefolder'; %change as appropriate
filepattern = 'Drive%02d.csv'; %change pattern if needed. Uses sprintf format string. Need a %d for file index
for iter = 1:numel(mydata)
%convert the scalar structure in each cell of mydata into a table and write to file
writetable(struct2table(mydata{iter}), fullfile(destination_folder, sprintf(filepattern, iter)));
end
Change the extension in filepattern to .xslx if you want to export as excel file instead.

More Answers (0)

Categories

Find more on Data Type Conversion 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!