# How to output entire columns of a cell array?

175 views (last 30 days)
Juan Rosado on 24 Dec 2012
I have this cell array and I want to output an entire column from the workspace.
The function goes like this,
function Time = Rin
fid = fopen( 'Rincon.txt' );
Time = textscan( fid, '%d%d%d%d%d%f%f%f%f%f%s%s%s%f%f' ...
, 'Delimiter' , ' ' ...
, 'CollectOutput' , true ...
, 'MultipleDelimsAsOne' , true ...
, 'Whitespace' , '' ...
);
fclose( fid );
end
When Run, the output is this,
ans =
[2163x5 int32] [2163x5 double] {2163x3 cell} [2163x2 double]
I want to illustrate on the Command Window the entire first column of the array, ans {1,2}
Jan on 24 Dec 2012
@Azzi: If you have an indexing bug in a program and confuse column with row vectors, using the explicit C{1,2} indexing can help to locate the problem. But this costs some processing time and it is less "nice".
The linear indexing is efficient for matrices also:
M = rand(2,2)
disp(M(4))
But this must be applied carefully and the explicite indexing is in most cases easier to debug. So finally, A{2} or A{1,2} is equivalent and it is a question of taste and the choice might depend on the decision, if the run time or the debug time is more important.

Matt J on 24 Dec 2012
ans{2}(:,1)
Laura Proctor on 24 Dec 2012
You can get the contents inside the cells out using curly braces. Once you do that, you index into the numeric array just like you would any variable.
Sometimes, it is easier for people to use intermediate variables to understand it better.
For example, here I am using an intermediate variable, x:
x = Time{2};
y = x(:,1);
Although doing this makes it easier for some to understand and is a good for learning, doing it all in one go, as Matt J has shown, is best practice.