Splitting up Numerical Text Data
显示 更早的评论
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 个评论
Cris LaPierre
2020-10-26
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
2020-10-26
hello
can you share some text data with your different data length
tx
Dalton Van Stratten
2020-10-26
Cris LaPierre
2020-10-27
编辑:Cris LaPierre
2020-10-27
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.
回答(1 个)
t = ' 89 93 96 99100100100 98 96 94 90';
T = textscan(t,'%3s','Whitespace','');
T = str2double(T{:})
2 个评论
J. Alex Lee
2020-10-27
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',''))
类别
在 帮助中心 和 File Exchange 中查找有关 String Parsing 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
