Extracting Vector from Cell Array Row or Column

조회 수: 31 (최근 30일)
R Stephens
R Stephens 2017년 7월 24일
편집: Jan 2017년 7월 24일
When trying to extract a row (or column) vector from a Cell Array it appears that the type of the cells in the Cell Array is determining whether or not a row vector can be extracted. Here's an example:
>> timeC = [timeB{1:end}];
>> class(timeC)
ans =
'char'
>>
As shown, the result does not produce a vector but instead a character array of all the cell array cells. What is the best way to extract a row or column from a cell array into a row or column vector. Also enclosed is a .mat file with the example cell array for converting to a row or column vector.
  댓글 수: 3
R Stephens
R Stephens 2017년 7월 24일
Actually I don't want to concatenate anything. I simply want to get a vector instead of a cell array of values. It appears however that for the .mat file enclosed in this example I am able to access elements in the timeB cell array by simply fetching them as timeB(n) so it appears that I can just treat this timeB cell array as an array for some reason.
Jan
Jan 2017년 7월 24일
편집: Jan 2017년 7월 24일
@R Stephens: See [EDITED] in my answer.

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

답변 (1개)

Jan
Jan 2017년 7월 24일
편집: Jan 2017년 7월 24일
When trying to extract a row (or column) vector from a Cell Array
Was does "extracting" mean? Your code:
[timeB{1:end}]
concatenates the contents of the cell elements, which are obviously char vectros in your case. But what is the wanted result? Perhaps you mean:
datenum(timeB)
See this:
C = {'a', 'b'; 'c', 'd'}
C_row1 = C(1, :)
C_col1 = C(:, 2)
[EDITED] After your comment:
This is a misunderstanding of the nature of cells. The shown cell contains char vectors. So you cannot obtain them "as a vector", because there are no vectors of vectors in Matlab. If you want to store a bunch of vectors, use either a matrix or a cell. In you case, timeB is a cell vector already and there is no more vector like container for these kind of data - except you want to concatenate the cell elements to a single string.
This is different, if the cell contains numbers:
C = {1,2,3}
v = [C{:}]
Now you get [1,2,3] as a vector. Clear?
it appears that I can just treat this timeB cell array as an array
for some reason.
Yes of course this cell array is an array also - of type cell. You can extract sub-cells by round parenthesis
a = timeB(2)
class(a)
or elements with curly braces
b = timeB{2}
class(b)

카테고리

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