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.
Thank you! That works perfectly.
Now I have the doubt about how to arrange the data by the source. I only know how to do it using if:
if source=="M1"
myM.M1 = [myM.M1; new_line];
elseif source=="M2"
myM.M2 = [myM.M2; new_line];
...
elseif source=="M8"
myM.M8 = [myM.M8; new_line];
end
I have to write only "M8" sources. But what If I have M20 or M100? Is there a proper way to do this, or am I in the correct direction?
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
Javier Mateo 2021년 1월 18일
편집: Javier Mateo 2021년 1월 18일
No, sometimes we miss some paket from the sources, thereby we do not have a new line every single second on every of them
Edit: Awesome, I did not know about dynamic fieldnames. Thank you!
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.
What is "a lot of data" when we talk about programming? (sorry.. not my field :D)
In this scenario, we have an struct of 8 fields, each of them can be up to '360x3 double'. Maybe more in the future...
Considering this struct is a global variable, how does it look like?
(I use global variable because this function is called by callback, so I guess there is no other way, is it?)
Stephen23
Stephen23 2021년 1월 18일
편집: Stephen23 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:
You might find this useful too:
Thank you very much for the recommendations.
I will consider about that if the programm becomes greater. For now it seems to be working flawless with globals.

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

답변 (0개)

질문:

2021년 1월 18일

댓글:

2021년 1월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by