I have a data as follows: This is the sampled data order from serial port which going to come using fgets(): line by line
% date:2016-08-05, time:10:05:23, t1:82.55,h1:85.60,t2:62.55,h2:65.60,
% date:2016-08-05, time:10:05:24, p1:20,p2:35,
% date:2016-08-05, time:10:05:25, p1:35,p2:21,
% date:2016-08-05, time:10:05:26, t1:45,h1:65.60,t2:75.55,h2:65.60,
rdstr='date:2016-08-05,time:10:05:23,t1:82.55,h1:85.60,t2:62.55,h2:65.60,'
Now, I would like to get the data to matrix var1 and var2.
var1= [datetime t1 t2;
datetime t1 t2]...
var2= [datetime h1 h2;
datetime h1 h2]
var3= [datetime p1 p2;
datetime p1 p2]...
Note: The readstr is a serial data received reading one line at a time. the var1 datetime must be at their particular sampling times.

6 comentarios

Geoff Hayes
Geoff Hayes el 13 de Mzo. de 2016
Mahesh - what is the data type for readstr? Is it a char or cell? Is it an array of strings or a single string? How do parse each element or have you yet to do that?
Mahesh
Mahesh el 13 de Mzo. de 2016
@Hayes: The data type is char. I have parse down using regexp function.
Geoff Hayes
Geoff Hayes el 13 de Mzo. de 2016
Mahesh - you have changed the format of your string and have removed the "rules" that you had before when creating/updating the other variables. Please clarify what the new algorithm is.
Mahesh
Mahesh el 13 de Mzo. de 2016
@Hayes: This is what I really need, help me with this updated one. FYI: I parsed down my string to individual matrices. i.e: all date time to for example,_date_[], all t1,t2, to Tem[] and so...
Geoff Hayes
Geoff Hayes el 13 de Mzo. de 2016
Mahesh - Please clarify what the new algorithm is. Use an example if necessary.
Mahesh
Mahesh el 13 de Mzo. de 2016
Editada: Mahesh el 13 de Mzo. de 2016
@Hayes: I attached my working code and data as .txt document. Here, I need to update the matrix var1 with all temperature values along with the datetime at their respective time. and same for the other two matrices. The issue here is time updated for every 5sec for temperature and humidity, and every one sec for pressure. which you can know by looking into my .txt document.

Iniciar sesión para comentar.

 Respuesta aceptada

Geoff Hayes
Geoff Hayes el 14 de Mzo. de 2016

0 votos

Mahesh - when I run your attached code, I observe errors with
if (out==1)
newdateTime1=newdateTime;
temperature=[newdateTime1 temp];
humidity=[newdateTime1 humi];
% temperature[1]=newdateTime1;
% humidity[1]=newdateTime1;
else
pressure=[newdateTime press];
end
because temp, humi, and press are not defined
Undefined function or variable "temp".
Please correct the code and confirm that you wish to create a variable that tracks the time and temperature, humidity, and pressure for all input (lines). If that is the case, then you could do something like
temperatureData = [];
humidityData = [];
pressureData = [];
fid = fopen('testdata.txt');
while ~iseof(fid)
collected=fgets(fid);
% do your work to figure out the datetime, and temperature t1 and t2
% update temperature data array
temperatureData = [temperatureData ; [newdateTime t1 t2]];
% etc.
end
fclose(fid);

2 comentarios

Mahesh
Mahesh el 15 de Mzo. de 2016
Hayes,I want to do exactly what you done. could you share how can we split [datetime t1,t2] and [datetime h1 h2]
Geoff Hayes
Geoff Hayes el 16 de Mzo. de 2016
If this is the line as read (from some source)
str = 'date:2016-08-05, time:10:05:28, temperature1:45.66,humidity1:54.60,temperature2:65.55,humidity2:22.55,';
then I think that you can split the string on the comma to divide into substrings as
subStrings = strsplit(str,',');
I think that you already have the code to determine the datetime from the first two elements (i.e. subStrings{1} and subStrings{2}), so you would then need to check to see what is the third element. Does it contain temperature1?
if ~isempty(regexp(subStrings{3},'temperature1:'))
t1 = str2double(strtrim(strrep(subStrings{3},'temperature1:','')));
end
The above is a rather clumsy approach to getting t1 but hopefully it can be adapted to suit your needs.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Characters and Strings en Centro de ayuda y File Exchange.

Etiquetas

Aún no se han introducido etiquetas.

Preguntada:

el 13 de Mzo. de 2016

Comentada:

el 16 de Mzo. de 2016

Community Treasure Hunt

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

Start Hunting!

Translated by