Issue with importing excel files

2 ビュー (過去 30 日間)
Matthew Brandsema
Matthew Brandsema 2014 年 11 月 24 日
コメント済み: Matthew Brandsema 2014 年 11 月 24 日
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 Center および File ExchangeData Type Conversion についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by