Issue with importing excel files

1 次查看(过去 30 天)
I posted a question similar to this, but I did not realize what was really going on until recently.
My excel file has multiple rows of the following form.
f 27//1 29//2 5//3 2//4
For this line, it is separated into 5 cells, which I will indicate with | signs.
f | 27//1 | 29//2 | 5//3 | 2//4
When I want to call the cell with 27//1 in it, i put..
txt(1,2)
and it gives me the following.
'27//1'
I need to extract the number to the left of the // sign. The problem is, I believe the single quotation marks from the import are messing things up. When I actually TYPE in 27//1 in the regexp function it works.
str2double(regexp('27//1','\d+(?=//)','match'))
However when I call the cell in the regexp it does NOT. I get NaN
str2double(regexp(txt(1,2),'\d+(?=//)','match'))
How can I circumvent this? I tried to search for a number between a ' and a // sign, but it didn't work.
str2double(regexp(txt(1,2),'(?<=")\d+(?=//)','match'))

采纳的回答

Geoff Hayes
Geoff Hayes 2014-11-24
Matthew - the problem isn't with the single quotes in your cell input, but with the str2double. Since you are passing a cell array (with only one string) as your input to regexp, the return value from that will be a cell array. For example,
>> var = {'27//1'};
>> regexp(var,'\d+(?=//)','match')
ans =
{1x1 cell}
If we look closer at ans we see that its value is
>> ans{1}
ans =
'27'
And since this answer is not a string but a cell, then the str2double returns NaN.
There are couple of ways to get around this:
  1. Pass the string from the cell array into the regexp using curly braces: str2double(regexp(txt{1,2},'\d+(?=//)','match'))
  2. Convert the cell element to a string: str2double(regexp(char(txt(1,2)),'\d+(?=//)','match'))
Try out the first method and see what happens!
  1 个评论
Matthew Brandsema
Matthew Brandsema 2014-11-24
Yes! The first one worked! Thank you for your reply and more importantly your description as to what is going on!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by