Issue with importing excel files

1 visualización (últimos 30 días)
Matthew Brandsema
Matthew Brandsema el 24 de Nov. de 2014
Comentada: Matthew Brandsema el 24 de Nov. de 2014
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'))

Respuesta aceptada

Geoff Hayes
Geoff Hayes el 24 de Nov. de 2014
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 comentario
Matthew Brandsema
Matthew Brandsema el 24 de Nov. de 2014
Yes! The first one worked! Thank you for your reply and more importantly your description as to what is going on!

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by