필터 지우기
필터 지우기

check cell's contents and convert to matrix

조회 수: 3 (최근 30일)
ben harper
ben harper 2011년 5월 27일
i have a data cell as below:
when i use
param1=cell2mat(data(:,2));
param2=cell2mat(data(:,6));
param3=cell2mat(data(:,7));
it gives error:
??? Error using ==> cell2mat at 47
All contents of the input cell array must be of the same data type.
So, every time there is a string in cell matrix MATLAB Gives error and i correct it by hand.
Is there a method that makes this:
param1=cell2mat(data(:,2));
If one of the members of cell is string, than assign that to value of zero??
Thank you very much
  댓글 수: 5
Walter Roberson
Walter Roberson 2011년 5월 27일
For me it appears in the section underneath "click to add tags". I had to temporarily allow tinypic.com but I did not have to allow any of the other sites the page wants (photobucket, gigya, z25, or others.)
http://oi56.tinypic.com/33ylu1z.jpg
should be the direct link.
Jan
Jan 2011년 5월 28일
Thanks Walter! Following your link I can see the table without any tricks. This is another argument to host pictures on a Mathworks server.

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

채택된 답변

Laura Proctor
Laura Proctor 2011년 5월 27일
The following code will find any strings in your input data and replace them with NaN values then converts the cell array to a matrix. You can use any value to replace the strings... it's just that NaN may be appropriate for this case.
c = { 1; 2 ; 3 ; 'aa ' ; 4 ; 10 ; ' '};
ival = cellfun(@ischar,c)
c(ival) = {NaN}
b = cell2mat(c)
  댓글 수: 1
Jan
Jan 2011년 5월 27일
The OP asked for zeros explicitely. CELLFUN(@ischar, C) and CELLFUN('isclass', C, 'char') is equivalent.

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

추가 답변 (6개)

Jan
Jan 2011년 5월 27일
data(cellfun('isclass', data, 'char')) = {0}

Walter Roberson
Walter Roberson 2011년 5월 27일
param1 = cellfun(@(c) isnumeric(c) * c(1),data(:,2))
  댓글 수: 1
Walter Roberson
Walter Roberson 2011년 5월 27일
Note: this also does the conversion from cell to array.

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


Andrei Bobrov
Andrei Bobrov 2011년 5월 28일
idx = find(cellfun('isclass',data,'char'));
data(idx) = data(idx-1)
  댓글 수: 1
Walter Roberson
Walter Roberson 2011년 5월 28일
Close, but that won't work if there are several strings in a row.

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


ben harper
ben harper 2011년 5월 28일
Thank you for answers.. What if i try to change the string cell to the prvious cell's value, not zero??
I mean
for i=1:size(data,1)
if data(i,2) is string
data(i,2)=data(i-1,2)
end
end
convert column to array with cell2mat
----------
Like this picture:

ben harper
ben harper 2011년 5월 29일
I wrote a solution as below: Can you recommend me if you know a shorter solution. Because in my code i must do this multiple times. Thank you all.
indices=cellfun('isclass', data(:,2), 'char');
for i=1:size(indices,1)
if indices(i)==1
if i>1
data(i,2)=data(i-1,2);
elseif i==1
data(i,2)=data(i+1,2);
end
end
end
data_new=cell2mat(data(:,2));

Laura Proctor
Laura Proctor 2011년 5월 31일
Are you guaranteed that there will not be two adjacent string values in the input matrix?
If so, then this should work for you:
if indices(1)
data(1,2) = data(2,2);
indices(1) = 0;
end
data(indices) = data([indices(2:end) 0]);

카테고리

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