MATLAB: Getting cell array of structs to a usable form in R

cell arrayr matlabstruct

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!

Best Answer

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.