check cell's contents and convert to matrix
古いコメントを表示
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 件のコメント
Jan
2011 年 5 月 27 日
I do not see a picture at the shown link.
An example could be helpful, e.g. {1,2,3,'String'} ?
Walter Roberson
2011 年 5 月 27 日
I didn't either at first, but then realized it was the table towards the top right.
Jan
2011 年 5 月 27 日
I tried it again without success. I see some GUI objects, "Images You'll also enjoy", some Tools on the left bottom. After I hit the "Next" button, a picture of a male human appeared in frontal plane - without cloths. I'm not sure if this should encourage me to disable the NoScript filter...
I do not find a table and I do think that TinyPic is junk.
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
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.
採用された回答
その他の回答 (6 件)
Jan
2011 年 5 月 27 日
data(cellfun('isclass', data, 'char')) = {0}
Walter Roberson
2011 年 5 月 27 日
param1 = cellfun(@(c) isnumeric(c) * c(1),data(:,2))
1 件のコメント
Walter Roberson
2011 年 5 月 27 日
Note: this also does the conversion from cell to array.
Andrei Bobrov
2011 年 5 月 28 日
idx = find(cellfun('isclass',data,'char'));
data(idx) = data(idx-1)
1 件のコメント
Walter Roberson
2011 年 5 月 28 日
Close, but that won't work if there are several strings in a row.
ben harper
2011 年 5 月 29 日
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]);
カテゴリ
ヘルプ センター および File Exchange で Cell Arrays についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!