MATLAB Answers

Splitting up Numerical Text Data

3 views (last 30 days)
Dalton Van Stratten
Dalton Van Stratten on 26 Oct 2020
Commented: Cris LaPierre on 27 Oct 2020
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

Show 1 older comment
Mathieu NOE
Mathieu NOE on 26 Oct 2020
hello
can you share some text data with your different data length
tx
Dalton Van Stratten
Dalton Van Stratten on 26 Oct 2020
I provided a screen capture below to help you become familiar with the format.
The problem rows that I am trying to utilize are TMP when temperature is equal to or exceeds 100 degrees, SKY when sky cover is 100%, PZR, PSN, PPL, PRA when probablity of precipitation falling as a specific type is 100%, and visibility when visibility is 100. Unfortunately, the format is basically broken whenever there is a 100 in the data.
And yes, the format you mentioned would be the correct example. It should be '89 93 96 99100100100 98 96 94 90' as an example. Does that change things?
The delimiter is a space in this case, but when there is a 100 in the data, there is no delimiter. Let's say I want to split the text '99100'. One could split that text at the 9 and the 1 but if we had '98100' that would have to be split at the 8 and the 1. Even if that is possible for any number combination, the next string could be '100100' which would be split at the 0 and the 1.
The only way I can think of solving this problem is using something like IF str2num('string') <= 100 check to see where 'string' should be logically split.
Cris LaPierre
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.

Sign in to comment.

Answers (1)

Cris LaPierre
Cris LaPierre on 27 Oct 2020
t = ' 89 93 96 99100100100 98 96 94 90';
T = textscan(t,'%3s','Whitespace','');
T = str2double(T{:})
T = 11×1
89 93 96 99 100 100 100 98 96 94

  2 Comments

J. Alex Lee
J. Alex Lee on 27 Oct 2020
is that any better than
T = cell2mat(textscan(t,'%3f','Whitespace',''));
?
Cris LaPierre
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',''))
T = 11×1
89 93 96 991 1 1 0 98 96 94

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!