creating numerical values from parts of a string read

I have the following problem, I use the following to read in a string in a column in excel
[numerics,strings] = xlsread (filename,'spreadsheet name','Axx:Ayy')
filename is not in qoutes because it is read in from a user input using
filename = sprintf('filename',answer)
looking at the strings array I can see that the characters are read into the array
I need to extract part of the string and convert it into numerical values
the strings looks like 0(07/24/2014 10:39:10.21304) I need the last part of the string 10.21304
My approach was to first separate the string using command
strsplit(strings (2,1)) where strings (2,1)
is the location in the matrix of the strings; the error I can getting is that
First input must be a string.
So clearly I don't understand the issue - I tried the following
str1 = strings (2,1) and the output is '2 (07/24/2014 10:39:10.21304)'
Does the presence of the qoutation marks have anythings to do with the error ?
Any insight as to how to resolve the problem would be appreciated. I have google
several approaches and have not found anything that I can try. My first issue
resolving the error 'First input must be a string'
Regards, Philip

 채택된 답변

Ben11
Ben11 2014년 8월 6일
If your strings are always of the same format, you can use regular expression to look for the colon followed by a number and followed by a dot, and then retrieve the numbers you want.
A = '0(07/24/2014 10:39:10.21304)'
ColonLocations = regexp(A,'\:\d*\.\d*','match')
which gives:
ColonLocations =
':10.21304'
Get rid of the colon:
ColonLocations{1}(1) = []
Convert to number:
ColonLocations = str2num(ColonLocations{1});
You can make this shorter by combining a few lines.
ColonLocations =
10.2130

댓글 수: 5

Note that the str2num function makes use of eval, and should therefore be replaced by str2double where possible. This is discussed here.
Evan thanks for the response,
ColonLocations = regexp(A,'\:\d*\.\d*','match') works and gave ':10.21304' as
you wrote however ColonLocations{1}(1) = [] did not get rid of the colon
in checking the contents, ColonLocations showed [] only
Do you have any suggestion on what I may be doing wrong?
Thanks in advance for your feedback
Regards, Philip
Mhh that's weird. You can use this as a workaround:
ColonLocations = ColonLocations{1}(2:end)
I'll play around to see what might cause the behavior you see.
Hi Evan so I found out what could be causing it, the first thing I have to do is to
str1 = char (strings (j,1)) where strings (j,1) contains A
then use str1 istead of A
then all the routins above works.
Thanks a lot for you help and sorry for the confusion.
Regards, Philip
glad it works then!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Evan
Evan 2014년 8월 6일
편집: Evan 2014년 8월 6일
Another option, using a lookaround operator.
A = '0(07/24/2014 10:39:10.21304)'
s = regexp(A,'(?<=:)\d+\.\d+','match');
n = str2double(s);
See more here.

댓글 수: 2

This works as well, the key is to change A with char - see above
Agains thanks for the help.
Regards, Philip
No problem!

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품

질문:

2014년 8월 6일

댓글:

2014년 8월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by