Check Existence of String

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 Comments

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?
@Hayes: The data type is char. I have parse down using regexp function.
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.
@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...
Mahesh - Please clarify what the new algorithm is. Use an example if necessary.
Mahesh
Mahesh on 13 Mar 2016
Edited: Mahesh on 13 Mar 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.

Sign in to comment.

 Accepted Answer

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 Comments

Hayes,I want to do exactly what you done. could you share how can we split [datetime t1,t2] and [datetime h1 h2]
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.

Sign in to comment.

More Answers (0)

Categories

Tags

No tags entered yet.

Asked:

on 13 Mar 2016

Commented:

on 16 Mar 2016

Community Treasure Hunt

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

Start Hunting!