필터 지우기
필터 지우기

How to convert the contents of a cell array into an array?

조회 수: 3 (최근 30일)
Sven
Sven 2013년 9월 10일
Hi, I have a cell array that looks a bit like this:
N='AA' 'AA_BB' 'AA_BB_CC' 'AA_BB_DD' 'AA_BB_EE'
it is easy enough to convert a specific entry of my cell into an array:
a=cell2mat(N(2)) a=AA_BB
Now I want to use the cellfun code in order to generate an array for all the entries in my cell. The end result I am envisioning looks a bit like:
a=AA AA_BB AA_BB_CC AA_BB_DD AA_BB_EE
I am trying the following code:
a=cellfun(cell2mat,N)
but I am getting an error message. Where am I going wrong? Thanks
  댓글 수: 2
Jan
Jan 2013년 9월 10일
편집: Jan 2013년 9월 10일
If you have a cell array, post the code for a cell array also:
N = {'AA', 'AA_BB', 'AA_BB_CC', 'AA_BB_DD', 'AA_BB_EE'};
When you get an error message, post a complete copy in the forum. It is much easier to solve a problem than to guess, what the problem is.
Sven
Sven 2013년 9월 10일
sure, sorry for not having posted it earlier...the error message I get looks as follows:
K>> a=cellfun(@cell2mat,N) Cell contents reference from a non-cell array object.
Error in cell2mat (line 43) cellclass = class(c{1});

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

답변 (1개)

Jan
Jan 2013년 9월 10일
편집: Jan 2013년 9월 10일
This is not a job for cellfun. And if it is one, a function handle would be required: cellfun(@cell2mat, ...) .
N = {'AA', 'AA_BB', 'AA_BB_CC', 'AA_BB_DD', 'AA_BB_EE'};
a = sprintf('%s ', N{:}); % Has a trailing space
% Or:
b = cat(2, N{:}); % No intermediate spaces
% Or:
c = CStr2String(N, ' ', 0);
with FEX: CStr2String, which inserts the separator between all strings except for the last string.
  댓글 수: 5
Sven
Sven 2013년 9월 11일
hmmm....while the example works fine for x=2, it gives me an error message if I use x=3 or any other higher value...Any idea what the problem might be?
cellfun(@(ii) ii(1:3),N,'un',0)
Index exceeds matrix dimensions.
Error in @(ii)ii(1:3)
Jan
Jan 2013년 9월 11일
cellfun(@(ii) ii(1:min(3, length(ii))),N,'un',0)

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

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by