How to log data without growing struct array?

조회 수: 8 (최근 30일)
Kevin Gilliam
Kevin Gilliam 2022년 4월 25일
댓글: Kevin Gilliam 2022년 4월 25일
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!

채택된 답변

Matt J
Matt J 2022년 4월 25일
편집: Matt J 2022년 4월 25일
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
Kevin Gilliam
Kevin Gilliam 2022년 4월 25일
Ah I hadn't considered that approach - very straight forward. Thanks!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Historical Contests에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by