Splitting up Numerical Text Data
6 views (last 30 days)
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 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)
Cris LaPierre
on 27 Oct 2020
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',''));
?
Cris LaPierre
on 27 Oct 2020
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',''))
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!