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
Jan
2011년 9월 20일
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?
답변 (6개)
Fangjun Jiang
2011년 9월 26일
1 개 추천
Use str2double() or str2num() instead. You are doing the opposite.
댓글 수: 4
schor
2011년 9월 26일
Fangjun Jiang
2011년 9월 26일
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
2011년 9월 26일
>> format long
>> str2double(A)
ans =
1.0e+009 *
1.319780805000000 1.319768288000000 1.319774249000000
Binod krushna
2023년 7월 27일
thanq so much
Tigersnooze
2011년 9월 20일
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).
Jan
2011년 9월 26일
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.
카테고리
도움말 센터 및 File Exchange에서 Data Type Conversion에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!