Using strfind or regexp in cell

2 views (last 30 days)
Linus Dock
Linus Dock on 11 Jun 2021
Commented: Rik on 11 Jun 2021
Hi!
I have the following example data:
'METAR ESSA 202008010020Z 28003KT CAVOK 10/09 Q1017 NOSIG'
'METAR ESSA 202008010050Z 27004KT CAVOK 10/09 Q1017 NOSIG'
'METAR ESSA 202008010120Z 27004KT CAVOK 09/09 Q1017 NOSIG'
'METAR ESSA 202008010150Z 28004KT CAVOK 10/09 Q1017 NOSIG'
'METAR ESSA 202008010220Z 29003KT CAVOK 09/09 Q1017 NOSIG'
'METAR ESSA 202008010250Z 29003KT CAVOK 09/09 Q1017 NOSIG'
'METAR ESSA 202008010320Z 28003KT CAVOK 09/09 Q1017 NOSIG'
'METAR ESSA 202008010350Z 03002KT CAVOK 12/12 Q1017 NOSIG'
'METAR ESSA 202008010420Z 29002KT CAVOK 11/11 Q1017 NOSIG'
'METAR ESSA 202008010450Z 34004KT CAVOK 13/12 Q1017 NOSIG'
'METAR ESSA 202008010520Z 30004KT CAVOK 15/13 Q1017 NOSIG'
'METAR ESSA 202008010550Z 32004KT CAVOK 16/13 Q1017 NOSIG'
'METAR ESSA 202008010620Z 34003KT CAVOK 17/14 Q1017 NOSIG'
'METAR ESSA 202008010650Z VRB03KT CAVOK 18/14 Q1017 NOSIG'
'METAR ESSA 202008010720Z VRB03KT CAVOK 19/13 Q1017 NOSIG'
'METAR ESSA 202008010750Z 05004KT 030V100 CAVOK 20/10 Q1017 NOSIG'
'METAR ESSA 202008010820Z 02004KT 350V060 CAVOK 20/09 Q1017 NOSIG'
'METAR ESSA 202008010850Z 03004KT 360V070 CAVOK 21/10 Q1018 NOSIG'
'METAR ESSA 202008010920Z 03004KT 360V090 CAVOK 21/08 Q1017 NOSIG'
'METAR ESSA 202008010950Z 03005KT 360V080 CAVOK 22/08 Q1017 NOSIG'
'METAR ESSA 202008011020Z 05005KT 010V090 CAVOK 22/08 Q1017 NOSIG'
I would like to use strfind or regexp to extract the group '10/09' from each string. This is the temperature and dewpoint of a certain airport at a certain time. I would also like to store the third group '202008010020Z' which is the date and time together with the temperature and dewpoint separated in a matrix. Could you please help me with this? I'm a bit stuck at the moment.
Best regards
Linus

Accepted Answer

Rik
Rik on 11 Jun 2021
Your intuition that you can use a regular expression is correct:
data={...
'METAR ESSA 202008010020Z 28003KT CAVOK 10/09 Q1017 NOSIG'
'METAR ESSA 202008010050Z 27004KT CAVOK 10/09 Q1017 NOSIG'
'METAR ESSA 202008010120Z 27004KT CAVOK 09/09 Q1017 NOSIG'
'METAR ESSA 202008010150Z 28004KT CAVOK 10/09 Q1017 NOSIG'
'METAR ESSA 202008010220Z 29003KT CAVOK 09/09 Q1017 NOSIG'
'METAR ESSA 202008010250Z 29003KT CAVOK 09/09 Q1017 NOSIG'
'METAR ESSA 202008010320Z 28003KT CAVOK 09/09 Q1017 NOSIG'
'METAR ESSA 202008010350Z 03002KT CAVOK 12/12 Q1017 NOSIG'
'METAR ESSA 202008010420Z 29002KT CAVOK 11/11 Q1017 NOSIG'
'METAR ESSA 202008010450Z 34004KT CAVOK 13/12 Q1017 NOSIG'
'METAR ESSA 202008010520Z 30004KT CAVOK 15/13 Q1017 NOSIG'
'METAR ESSA 202008010550Z 32004KT CAVOK 16/13 Q1017 NOSIG'
'METAR ESSA 202008010620Z 34003KT CAVOK 17/14 Q1017 NOSIG'
'METAR ESSA 202008010650Z VRB03KT CAVOK 18/14 Q1017 NOSIG'
'METAR ESSA 202008010720Z VRB03KT CAVOK 19/13 Q1017 NOSIG'
'METAR ESSA 202008010750Z 05004KT 030V100 CAVOK 20/10 Q1017 NOSIG'
'METAR ESSA 202008010820Z 02004KT 350V060 CAVOK 20/09 Q1017 NOSIG'
'METAR ESSA 202008010850Z 03004KT 360V070 CAVOK 21/10 Q1018 NOSIG'
'METAR ESSA 202008010920Z 03004KT 360V090 CAVOK 21/08 Q1017 NOSIG'
'METAR ESSA 202008010950Z 03005KT 360V080 CAVOK 22/08 Q1017 NOSIG'
'METAR ESSA 202008011020Z 05005KT 010V090 CAVOK 22/08 Q1017 NOSIG'};
A=regexp(data,'\d{12}[A-Z]','match');A=[A{:}]
A = 1×21 cell array
{'202008010020Z'} {'202008010050Z'} {'202008010120Z'} {'202008010150Z'} {'202008010220Z'} {'202008010250Z'} {'202008010320Z'} {'202008010350Z'} {'202008010420Z'} {'202008010450Z'} {'202008010520Z'} {'202008010550Z'} {'202008010620Z'} {'202008010650Z'} {'202008010720Z'} {'202008010750Z'} {'202008010820Z'} {'202008010850Z'} {'202008010920Z'} {'202008010950Z'} {'202008011020Z'}
B=regexp(data,'\d{2}/\d{2}','match');B=[B{:}]
B = 1×21 cell array
{'10/09'} {'10/09'} {'09/09'} {'10/09'} {'09/09'} {'09/09'} {'09/09'} {'12/12'} {'11/11'} {'13/12'} {'15/13'} {'16/13'} {'17/14'} {'18/14'} {'19/13'} {'20/10'} {'20/09'} {'21/10'} {'21/08'} {'22/08'} {'22/08'}
  3 Comments
Linus Dock
Linus Dock on 11 Jun 2021
Thanks that's great!
Now I'm making some progress :)
Now I have a follow-up question,
How do I get the values into one cell.
I would like to have an output as a table with:
(Dateandtime, Temperature, Dewpoint) in separate kolumns.
I've tried to modify the above regexpr to sort out the temperature and use the following code to create a table.
How can I organise the output to create a table of the data?
A=regexp(METARresult,'\d{12}[A-Z]','match');A=[A{:}]
B=regexp(METARresult,'\d{2}(?=/)','match');B=[B{:}]
C={A B};
Z=cell2table(C);
writetable(Z,'data.txt');
winopen('data.txt');
Best regards
Linus
Rik
Rik on 11 Jun 2021
You should try to parse the text to the data type you need. Then you can easily put them together. A loop will probably be best. You should not need to write to a file as an intermediate step.

Sign in to comment.

More Answers (0)

Categories

Find more on Characters and Strings in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!