I have an array that is receiving data from the cloud, and I'm loosing the initial data while the array continue receiving the data.
How can I concatenate the data to keep the initial data in the array while continue receiving new data?
Here's the code and the array as a reference.
while true
dataTT = (read(MQTTSignal));
str = dataTT.Data;
str = strrep(str, '{', ''); % remove opening bracket
str = strrep(str, '}', ''); % remove closing bracket
str = split(str,",");
current = str2double(str(:,1));
speed = str2double(str(:,2));
time = str2double(str(:,3));
subplot(2,1,1);
plot(time,current);
ylim([0,150]);
grid ON
title("Current");
subplot(2,1,2);
plot(time,speed);
ylim([0,1000]);
grid ON
title("Speed");
drawnow
pause(3)
end

 Respuesta aceptada

Matt J
Matt J el 29 de Mzo. de 2023
dataTT=[];
while true
dataTT = [dataTT, read(MQTTSignal)];
str = dataTT(end).Data;
...
end

7 comentarios

Luis Ricardo Rivera Goitia
Luis Ricardo Rivera Goitia el 29 de Mzo. de 2023
It got me an error msg
Error using ()
Subscripting into a table using one subscript (as in t(i)) is not supported. Specify a row subscript and a variable
subscript, as in t(rows,vars). To select variables, use t(:,i) or for one variable t.(i). To select rows, use t(i,:).
Error in MQTT_ESP32_Current_Speed_Graphs_2nd_Trial (line 21)
str = dataTT(end).Data;
Matt J
Matt J el 30 de Mzo. de 2023
Editada: Matt J el 30 de Mzo. de 2023
Apparently your dataTT is not in struct form.
dataTT = [dataTT, table2struct( read(MQTTSignal) )];
Luis Ricardo Rivera Goitia
Luis Ricardo Rivera Goitia el 30 de Mzo. de 2023
Using table2struct function gave me also an error and has changed the str array to only three rows of data (separated)
Index in position 2 exceeds array bounds. Index must not exceed 1.
Error in MQTT_ESP32_Current_Speed_Graphs_2nd_Trial (line 30)
speed = str2double(str(:,2));
I changed the code to the following and I got the graphs for a moment until the program fails giving me this error.
Error using tabular/horzcat
All tables being horizontally concatenated must have the same number of rows.
Error in MQTT_ESP32_Current_Speed_Graphs_2nd_Trial (line 20)
dataTT = [dataTT,(read(MQTTSignal))];
dataTT=[];
while true
dataTT = [dataTT,(read(MQTTSignal))];
%dataTT = [dataTT, table2struct( read(MQTTSignal) )];
str = dataTT.Data;
str = strrep(str, '{', ''); % remove opening bracket
str = strrep(str, '}', ''); % remove closing bracket
str = split(str,",");
current = str2double(str(:,1));
speed = str2double(str(:,2));
time = str2double(str(:,3));
Matt J
Matt J el 30 de Mzo. de 2023
dataTT = [dataTT, table2struct( read(MQTTSignal,"ToScalar",1) )];
Luis Ricardo Rivera Goitia
Luis Ricardo Rivera Goitia el 30 de Mzo. de 2023
It gave me this error that is not recognized
Error using MQTT_para_Appdesigner
'ToScalar' is not a recognized parameter. For a list of valid name-value pair arguments, see the documentation for this function.
Matt J
Matt J el 31 de Mzo. de 2023
dataTT = [dataTT, table2struct( read(MQTTSignal) ,"ToScalar",1 )];
Luis Ricardo Rivera Goitia
Luis Ricardo Rivera Goitia el 10 de Abr. de 2023
Apparently it worked, the only thing is that dataTT is now divided in two, and one of this two is cumulating the data in segments, I think that would help me to avoid loosing the previous information, thx.
Here's an image about its behavior:
As you can see Data column is storing the dataTT received from MQTT in segments.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Productos

Versión

R2022b

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by