How to log data without growing struct array?

8 Ansichten (letzte 30 Tage)
Kevin Gilliam
Kevin Gilliam am 25 Apr. 2022
Kommentiert: Kevin Gilliam am 25 Apr. 2022
I am writing an app which is receives JSON-encoded data over a UDP connection and does a few things with it:
  1. Parses it with jsondecode()
  2. Logs the parsed struct to a file
  3. Updates an Axes object in the GUI with a few selected fields in the struct with the new data points
The incoming data has no nested structs, but the field names and the number of fields are not known beforehand. Also the fields don't change over time so the field names in the first received message are representative of all the subsequent messages.
I know it's not good to continuously grow a structure array so I was thinking when I get the first message I would try to create a struct array "buffer", say 1000 elements deep, where each element has all the fields that the first message had, and then every 1000 messages it would log the entire buffer's data to the filesystem, update the plot with those 1000 points, and then reset the buffer indexing variable to one. I am having a lot of trouble trying to figure out how to initialize this struct-array buffer though.
I have tried creating the struct array like this:
firstMessageFlag = true;
msgBuff(1000) = struct()
rxMsgStruct = jsondecode(msgStr);
if firstMessageFlag
fn = fieldnames(rxMsgStruct);
for ii = 1:numel(fn)
% First thing I tried:
msgBuff(:).(fn{ii}) = [];
% Second thing I tried:
msgBuff(end) = rxMsgStruct;
end
firstMessageFlag = false;
end
...and a few other variations on one of those two themes, but I can't get it to work. Only thing that works is to not preallocate msgBuff and just keep appending the new message to a struct array.
What am I not understanding? Thanks!

Akzeptierte Antwort

Matt J
Matt J am 25 Apr. 2022
Bearbeitet: Matt J am 25 Apr. 2022
I don't see any reason why your method wouldn't work, but I like this better:
firstMessageFlag = true;
emptyStruct = structfun(@(s) [], jsondecode(msgStr) ,'uni',0) ;
if firstMessageFlag
msgBuff=repmat( emptyStruct ,1000,1);
firstMessageFlag = false;
end
  1 Kommentar
Kevin Gilliam
Kevin Gilliam am 25 Apr. 2022
Ah I hadn't considered that approach - very straight forward. Thanks!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Historical Contests finden Sie in Help Center und File Exchange

Produkte


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by