Find string corresponding to another string within a text file

2 views (last 30 days)
Hi,
I have a text file,
I was trying to read the latitide, longitude and station name corresponding to the associated strings within a text file. For example for reading the latitude and longitude in the file, the below lines worked.
buffer = fileread(fullFileName) ;
lat=str2double(regexpi(buffer, '(?<=Station latitude:\s+) [\d\.]+', 'match'));
lon=str2double(regexpi(buffer, '(?<=Station longitude:\s+) [\d\.]+', 'match'));
However, for station name which is in the .txt file (attached) as Station name: Payerne,
the below line did not work and I am getting NaN as output
name=str2double(regexpi(buffer, '(?<=Station name:\s+) \w*', 'match'));

Accepted Answer

Mohammad Sami
Mohammad Sami on 17 Jul 2020
Edited: Mohammad Sami on 17 Jul 2020
Station name is a char / string. however you are using str2double. This will ofcourse give you a nan output.
You can remove the str2double.
name=regexpi(buffer, '(?<=Station name:\s+)\w*', 'match');

More Answers (0)

Categories

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

Community Treasure Hunt

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

Start Hunting!