Extracting Vector from Cell Array Row or Column

34 Ansichten (letzte 30 Tage)
R Stephens
R Stephens am 24 Jul. 2017
Bearbeitet: Jan am 24 Jul. 2017
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 Kommentare
R Stephens
R Stephens am 24 Jul. 2017
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 am 24 Jul. 2017
Bearbeitet: Jan am 24 Jul. 2017
@R Stephens: See [EDITED] in my answer.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Jan
Jan am 24 Jul. 2017
Bearbeitet: Jan am 24 Jul. 2017
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)

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by