Getting cell array of structs to a usable form in R
조회 수: 14 (최근 30일)
이전 댓글 표시
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
Guillaume
2019년 11월 27일
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?
채택된 답변
Guillaume
2019년 11월 27일
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.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Type Conversion에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!