Borrar filtros
Borrar filtros

Convert Json cells/struct to table

14 visualizaciones (últimos 30 días)
Dion Theunissen
Dion Theunissen el 10 de Mayo de 2022
Respondida: Ravi el 5 de Oct. de 2023
I have an API request and want to convert in in a table.
The response is a cell build out of structs. Every row in de cell needs to be a row in the table. The structs are build out of charts, doubles and structs again. Now i made the following but it looks like this is not the right and quickest way. I fill the RawDataAPI (empty table) by getting every value one by one.
for i2 = 1:size(response3)
RawDataAPI.triggerType(i2) = string(response3{i2, 1}.triggerType);
RawDataAPI.vehicleIdentificationNumber(i2) = string(response3{i2, 1}.vehicleIdentificationNumber);
RawDataAPI.UniekId(i2) = string(response3{i2, 1}.driverIdentification);
RawDataAPI.createdTimestamp(i2) = string(response3{i2, 1}.createdTimestamp);
RawDataAPI.receivedTimestamp(i2) = string(response3{i2, 1}.receivedTimestamp);
RawDataAPI.distance(i2) = (response3{i2, 1}.distance);
RawDataAPI.distanceCruiseControl(i2) = (response3{i2, 1}.distanceCruiseControl);
RawDataAPI.weight(i2) = (response3{i2, 1}.weight);
RawDataAPI.fuelUsed(i2) = (response3{i2, 1}.fuelUsed);
RawDataAPI.fuelUsedSpeedZero(i2) = (response3{i2, 1}.fuelUsedSpeedZero);
RawDataAPI.fuelUsedPto(i2) = (response3{i2, 1}.fuelUsedPto);
RawDataAPI.durationUsedPto(i2) = (response3{i2, 1}.durationPto);
RawDataAPI.durationSpeedOverZero(i2) = (response3{i2, 1}.durationSpeedOverZero);
RawDataAPI.durationSpeedZero(i2) = (response3{i2, 1}.durationSpeedZero);
RawDataAPI.distanceCoasting(i2) = (response3{i2, 1}.distanceCoasting);
RawDataAPI.durationCruiseControl(i2) = (response3{i2, 1}.durationCruiseControl);
RawDataAPI.brakeCounter(i2) = (response3{i2, 1}.brakeCounter);
RawDataAPI.latitude(i2) = (response3{i2, 1}.latitude);
RawDataAPI.longitude(i2) = (response3{i2, 1}.longitude);
RawDataAPI.altitude(i2) = (response3{i2, 1}.altitude);
RawDataAPI.Acc03(i2) = [response3{i2, 1}.acceleration(1).value].';
RawDataAPI.Acc05(i2) = [response3{i2, 1}.acceleration(2).value].';
RawDataAPI.Acc07(i2) = [response3{i2, 1}.acceleration(3).value].';
RawDataAPI.Acc09(i2) = [response3{i2, 1}.acceleration(4).value].';
RawDataAPI.Acc11(i2) = [response3{i2, 1}.acceleration(5).value].';
RawDataAPI.Acc15(i2) = [response3{i2, 1}.acceleration(6).value].';
RawDataAPI.Acc2(i2) = [response3{i2, 1}.acceleration(7).value].';
RawDataAPI.Acc25(i2) = [response3{i2, 1}.acceleration(8).value].';
RawDataAPI.Acc3(i2) = [response3{i2, 1}.acceleration(9).value].';
RawDataAPI.Acc35(i2) = [response3{i2, 1}.acceleration(10).value].';
RawDataAPI.Acc4(i2) = [response3{i2, 1}.acceleration(11).value].';
RawDataAPI.Brake03(i2) = [response3{i2, 1}.brake(1).value].';
RawDataAPI.Brake05(i2) = [response3{i2, 1}.brake(2).value].';
RawDataAPI.Brake07(i2) = [response3{i2, 1}.brake(3).value].';
RawDataAPI.Brake09(i2) = [response3{i2, 1}.brake(4).value].';
RawDataAPI.Brake11(i2) = [response3{i2, 1}.brake(5).value].';
RawDataAPI.Brake15(i2) = [response3{i2, 1}.brake(6).value].';
RawDataAPI.Brake2(i2) = [response3{i2, 1}.brake(7).value].';
RawDataAPI.Brake25(i2) = [response3{i2, 1}.brake(8).value].';
RawDataAPI.Brake3(i2) = [response3{i2, 1}.brake(9).value].';
RawDataAPI.Brake35(i2) = [response3{i2, 1}.brake(10).value].';
RawDataAPI.Brake4(i2) = [response3{i2, 1}.brake(11).value].';
RawDataAPI.Torque1(i2) = [response3{i2, 1}.torque(1).value].';
RawDataAPI.Torque2(i2) = [response3{i2, 1}.torque(2).value].';
RawDataAPI.Torque3(i2) = [response3{i2, 1}.torque(3).value].';
RawDataAPI.Torque4(i2) = [response3{i2, 1}.torque(4).value].';
RawDataAPI.Torque5(i2) = [response3{i2, 1}.torque(5).value].';
RawDataAPI.Torque6(i2) = [response3{i2, 1}.torque(6).value].';
RawDataAPI.Torque7(i2) = [response3{i2, 1}.torque(7).value].';
RawDataAPI.Torque8(i2) = [response3{i2, 1}.torque(8).value].';
RawDataAPI.Torque9(i2) = [response3{i2, 1}.torque(9).value].';
RawDataAPI.Torque10(i2) = [response3{i2, 1}.torque(10).value].';
RawDataAPI.Speed1(i2) = [response3{i2, 1}.speed(1).value].';
RawDataAPI.Speed2(i2) = [response3{i2, 1}.speed(2).value].';
RawDataAPI.Speed3(i2) = [response3{i2, 1}.speed(3).value].';
RawDataAPI.Speed4(i2) = [response3{i2, 1}.speed(4).value].';
end

Respuestas (1)

Ravi
Ravi el 5 de Oct. de 2023
Hi Dion Theunissen,
According to my understanding, you want to shorten your code avoiding writing redundant statements.
You can use the “eval function for this purpose. This can be broken down into few steps.
  • Find the properties that are present in the struct using “fieldnames function and store them in an array.
  • Since, now you have the field names. You can access the field by writing an expression in the eval function.
myCourse = struct("coursename", "MATLAB", "number_of_students", 100, "duration", "6 weeks");
required = 'duration';
ans = eval(['myCourse.', char(required)]);
disp(ans);
The eval function here evaulates the expression, “myCourse.duration” and stores that answer in “ans” variable.
  • Loop over the field names and assign them to the corresponding table variable name.
% triggerType
var = 'triggerType';
eval(["RawDataAPI.", char(var), "(i) = string(response3(", num2str(i), ".", char(var)]);
% torque
for j=1:10
eval(["RawDataAPI.Torque", num2str(j), "(", num2str(i), ") = [response3{", num2str(i), ", 1}.torque(", num2str(j), ").value"]);
end
Though this helps you shorten your code a bit, yet there are some statements that you need to write manually because there is no pattern in the variable names of the table, for example “Acc”, “Brake”.
For more understanding about the eval function kindly refer to the below provided links.
Hope this helps.

Categorías

Más información sobre Numeric Types en Help Center y File Exchange.

Productos


Versión

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by