Extracting Vector from Cell Array Row or Column

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 件のコメント

Stephen23
Stephen23 2017 年 7 月 24 日
編集済み: Stephen23 2017 年 7 月 24 日
@R Stephens: your cell array timeB contains only 1X9 char vectors:
>> cellfun('isclass',timeB,'char')
ans =
1 1 1 1 1 1 1 1 1
>> cellfun('size',timeB,1)
ans =
1 1 1 1 1 1 1 1 1
>> cellfun('size',timeB,2)
ans =
9 9 9 9 9 9 9 9 9
and as such any horizontal concatenation of those 1x9 char vectors will simply produce a longer char vector. Although you write "the result does not produce a vector", the output is most certainly a vector (how did you check?):
>> timeC = [timeB{1:end}];
>> size(timeC)
ans =
1 81
So what you have done is shown us that horizontally concatenating char vectors together horizontally concatenates char vectors together. It is unclear what your problem is with this. How do you expect horizontal concatenation to work?
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 日

0 投票

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)

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

タグ

質問済み:

2017 年 7 月 24 日

編集済み:

Jan
2017 年 7 月 24 日

Community Treasure Hunt

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

Start Hunting!

Translated by