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!