Splitting up Numerical Text Data
Show older comments
I am using the webread function to obtain numerical text data from a webpage. Each row of data represents a different weather element while each column represents the forecast time. I have had success spliting up the numerical data from rows like TMP, DPT, and WSP (Temperature, Dew point, and Wind Speed) where the numbers usually only have two digits. However, when rows like TMP, SKY, VIS (Temperature, Sky Cover, Visiblity) have three digits, the text may appear: '89 93 96 9910010010098 96 94 90' and using str2num will output 89 93 96 9910010010098 96 94 90 when I want it to read 89 93 96 99 100 100 100 98 96 94 90 so I can plot the values. I've been using str2num for two digit numbers and it works great. Given that the string of numerical text can change for a different forecast time period and location, how can I write a section of code that splits up this data?
4 Comments
Cris LaPierre
on 26 Oct 2020
The issue here appears to be there is no clear delimiter between your numbers. Even though this data appears to be fixed width, the format changes. With it not being consistant, there is no simple fix I'm aware of.
Are you positive you copied the data correctly? Is there any chance it should be this?
'89 93 96 99100100100 98 96 94 90'
Mathieu NOE
on 26 Oct 2020
hello
can you share some text data with your different data length
tx
Dalton Van Stratten
on 26 Oct 2020
Cris LaPierre
on 27 Oct 2020
Edited: Cris LaPierre
on 27 Oct 2020
Yes, that does change things. It means the data is fixed width, which makes it possible to separate. I therefore believe it is actually ' 89 93 96 99100100100 98 96 94 90'.
Answer below.
Answers (1)
t = ' 89 93 96 99100100100 98 96 94 90';
T = textscan(t,'%3s','Whitespace','');
T = str2double(T{:})
2 Comments
J. Alex Lee
on 27 Oct 2020
is that any better than
T = cell2mat(textscan(t,'%3f','Whitespace',''));
?
I had tried %f, but for me, it wasn't capturing the numbers correctly.
t = ' 89 93 96 99100100100 98 96 94 90';
T = cell2mat(textscan(t,'%3f','Whitespace',''))
Categories
Find more on String Parsing 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!