Real time growing table. Should I use table, double array, cell array..?
이전 댓글 표시
I am reading data from a serialport using Callback.
So every line of data is a 'double 1x3' like:
new_line = [time distance RSSI]
Every second I get around 6-8 lines and I need to store them and plot in real time ( plot(time,distance) )
It can be easily done with matrix like:
M(end+1,:)=new_line
However, I have to separate into 8 different matrix because the data is gathered from different sources. One easy way is to make 8 different matrix (M1, M2... M8)
But I wonder if I can store all in the same variable (a table)? The goal would be something looking like this:
T =
8×2 table
M1 M2
_____________________________ _____________________________
0.20619 249 -35 0.53786 257 -35
1.214 249 -35 1.5412 238 -35
2.2208 221 -35 2.537 235 -35
3.2199 262 -35 3.5289 268 -35
4.2186 240 -35 4.5363 226 -35
5.2124 252 -35 5.557 244 -35
6.2206 226 -35 6.5375 230 -35
7.2237 242 -35 7.5445 230 -35
But for 8 different variables, and increasing rows (size not known before hand)
But then, how do I store every new line of data in the specific "source" column, without affecting the other sources column? Am I in the wrong direction?
Thank you very much in advance
댓글 수: 8
One efficient way is to use struct.
myM = struct;
myM.M1 = [myM.M1; newData4M1];
myM.M2 = [myM.M2; newData4M2];
So each field can grow independently. Of course, you can also use cell in a similar way.
Javier Mateo
2021년 1월 18일
In the end, will all of the matrices have the same number of rows?
"Is there a proper way to do this, or am I in the correct direction?"
You should replace that IF/ELSEIF construct with dynamic fieldnames:
myM.(source) = [myM.(source);new_line];
Javier Mateo
2021년 1월 18일
편집: Javier Mateo
2021년 1월 18일
J. Alex Lee
2021년 1월 18일
if you need to write to disk and/or there's lots of data, maybe consider using a database instead of holding a matlab variable in memory? I've played around with sqlite using mksqlite with some success.
Javier Mateo
2021년 1월 18일
"What is "a lot of data" when we talk about programming?"
I would say "when it gets close to the size of your available memory".
"I use global variable because this function is called by callback, so I guess there is no other way"
Gobal variables are not a reliable or efficient way to pass data between callbacks (or for that matter between any functions). Global variables are avoided by experienced programmers because their usage wastes programming and debugging time.
The recommended approaches are given here:
https://www.mathworks.com/help/matlab/creating_guis/share-data-across-callbacks-in-app-designer.html
You might find this useful too:
Javier Mateo
2021년 1월 19일
답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Serial and USB Communication에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!