How to log data without growing struct array?
9 次查看(过去 30 天)
显示 更早的评论
I am writing an app which is receives JSON-encoded data over a UDP connection and does a few things with it:
- Parses it with jsondecode()
- Logs the parsed struct to a file
- 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!
0 个评论
采纳的回答
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!