Cell array to ordinary array. cell2mat

조회 수: 5 (최근 30일)
Daan
Daan 2015년 9월 21일
편집: Stephen23 2015년 9월 21일
Hi.
I have got a cell array C, but I would like to convert it to a ordinary array. When I try do it with the function cell2mat I get a 1 by 1 matrix with in spot 1,1 all numbers seperated by comma's. (probably because the array originally came from a .cvs file.) How to I get a 1 by 7 ordinary array?
C = {'0,800385595', '16,75872765', '-11,99131211', '3,884404324', ...
'2,127166729', '0,917013004', '9,531417436'}
Many Thanks, Daan

채택된 답변

Stephen23
Stephen23 2015년 9월 21일
편집: Stephen23 2015년 9월 21일
You can use str2double inside cellfun, like this:
>> C = {'0,800385595', '16,75872765', '-11,99131211', '3,884404324', '2,127166729', '0,917013004', '9,531417436'};
>> N = cellfun(@str2double,strrep(C,',','.'))
Also note that if the original data is in a .csv file and uses a decimal point (not a comma), then this data can be read as directly as numeric values anyway, without requiring conversion to numeric after reading them into MATLAB. You can have a look at textscan, or all of the other text-file reading functions.
  댓글 수: 1
Jan
Jan 2015년 9월 21일
편집: Jan 2015년 9월 21일
This is not spam, of course. When I hit the "This is not spam" button, I get an error message, that "something went wrong". Strange. Ah, most likely someone else hit this button before already.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Jan
Jan 2015년 9월 21일
It is fast to concatenate the strings and parse them afterwards at once:
C = {'0,800385595', '16,75872765', '-11,99131211', '3,884404324', ...
'2,127166729', '0,917013004', '9,531417436'};
C = strrep(C, ',', '.'); % Dots as decimal separator!!!
S = sprintf('%s*', C{:});
D = sscanf(S, '%f*');

카테고리

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