Split array of strings and convert to numbers?

Hello all, reading in a 2-columned spreadsheat file using xlsread gives me the following text-string (I couldn't manage this function to read in directly to numbers): '0,000' '1319,779017' '1,000' '1319,776931' '2,000' '1319,780805' '3,000' '1319,783785' ...and so on. Now, I want to convert column 1 into an array of numbers - let's say X, and column 2 into an array of numbers, let's say Y. Which function could I use for this? Many thanks in advance

1 comentario

Jan
Jan el 20 de Sept. de 2011
What is column 1 and column 2? Is the comma the separator for the decimals? Does "text-string" mean, that the data are a cell string?

Iniciar sesión para comentar.

Respuestas (6)

Fangjun Jiang
Fangjun Jiang el 26 de Sept. de 2011

1 voto

Use str2double() or str2num() instead. You are doing the opposite.

4 comentarios

schor
schor el 26 de Sept. de 2011
Ah, of course, I was using str2num and str2double. The former doesn't work on arrays at all, the latter truncates the strings. So, do you have any other ideas how this could be done?
Fangjun Jiang
Fangjun Jiang el 26 de Sept. de 2011
What do you mean "doesn't work". What is your matrix A (in what format)?
If A is a cell array of string, see below.
>> A = {'1319,780805', '1319,768288', '1319,774249'};
>> str2double(A)
ans =
1.0e+009 *
1.3198 1.3198 1.3198
Fangjun Jiang
Fangjun Jiang el 26 de Sept. de 2011
>> format long
>> str2double(A)
ans =
1.0e+009 *
1.319780805000000 1.319768288000000 1.319774249000000
Binod krushna
Binod krushna el 27 de Jul. de 2023
thanq so much

Iniciar sesión para comentar.

Tigersnooze
Tigersnooze el 20 de Sept. de 2011
What does your code look like? Normally when I use xlsread with a multi-column spreadsheet I get a multi-column array in return.
Something like:
[num, txt, dummy] = xlsread(filename, sheet)
Will return an m-by-n array in num, depending on the rows and columns in the sheet. I think you may also be running into problems because it looks like you have multiple values in each cell separated by commas, rather than two columns (if I'm reading that correctly--and assuming you're not using European-style numbers).
schor
schor el 26 de Sept. de 2011
Hi, I did exactly that, but it didn't work. Have I stored my data in an xls-format too new for Matlab (2007b)? Please find attached the xls file (which was automatically generated using LabVIEW). I get the following error message (running on Linux)
[num, txt, dummy] = xlsread(filename, sheet)
>> [num, txt, dummy] = xlsread('10mMHis_2mMTartaric_A_10_0.xls', 1)
Warning: XLSREAD has limited import functionality on non-Windows platforms
or in basic mode. Refer to HELP XLSREAD for more information.
> In xlsread at 201
??? Error using ==> biffread at 52
Unable to read XLS file
Ok now I've just realised that I can't attach anything here, maybe you guys could send me your email address? Many thanks!
schor
schor el 26 de Sept. de 2011
After some further examination, my question boils down to the point how an array of strings can be converted into an array of numbers:
A= '1319,780805'
'1319,768288'
'1319,774249'
...and so on, B=num2str(A) doesn't work, B=num2double(A) also doesn't work. Any ideas? Many thanks!

1 comentario

Jan
Jan el 26 de Sept. de 2011
Is this a cell string or CHAR matrix? Is the comma the decimal separator or a kind of column separator?

Iniciar sesión para comentar.

Jan
Jan el 26 de Sept. de 2011
If A is a cell string and the comma is the decimal point:
A = {'1319,780805', '1319,768288', '1319,774249'};
A = strrep(A, ',', '.');
S = sprintf('%s*', A{:});
Num = sscanf(S, '%g*');
This is very fast even for large arrays.
schor
schor el 27 de Sept. de 2011

0 votos

Thank you very much, that did the trick!

Categorías

Más información sobre Data Type Conversion en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 20 de Sept. de 2011

Comentada:

el 27 de Jul. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by