How to extract a column from cell arrays matrix?
Show older comments
Dear MATLAB users,
I wish I got your help for this problem. I have built a cell arrays as a matrix as follow
for k=1:5
out{k}(1,:}=
out{k}(2,:}=
out{k}(3,:}=
end
I am trying to extract the the first element in first array from each cell. i have tried this out{1:5}(1,1) but it does not work however if i type out{1}(1,1) this give the first element but in specific cell also out{1:5} shows all cell matrices. The error I saw when i tried out{1:5}(1,1) was Expected one output from a curly brace or dot indexing expression, but there were 5 results.
Thank you in advance. Best Aziz
3 Comments
Stephen23
on 29 Apr 2017
You should consider simplifying how your data is arranged. Simpler data means simpler code. Simpler code means code is easier to write, and has fewer bugs.
For example this data might be better stored in an ND cell array, or perhaps even a simple numeric array (vector/matrix/ND).
Abdulaziz Abutunis
on 29 Apr 2017
Walter Roberson
on 29 Apr 2017
Abdulaziz Abutunis comments to Stephen:
He did not solve the problem but he did give a good advice to avoid this problem in the future. he even gave more than one option.
Accepted Answer
More Answers (2)
Image Analyst
on 29 Apr 2017
Edited: Image Analyst
on 29 Apr 2017
1 vote
You can't have out{k}(1,:} because you got two closing braces and no closing parenthesis. Maybe you meant out{k}(1,:). See the FAQ: http://matlab.wikia.com/wiki/FAQ#What_is_a_cell_array.3F and try to avoid cell arrays in most cases, definitelyif you have rectangular numerical array of constant dimensions, it just complicates things.
1 Comment
Abdulaziz Abutunis
on 29 Apr 2017
Juvinch Vicente
on 15 Aug 2018
Edited: Juvinch Vicente
on 15 Aug 2018
1 vote
I'm not sure if this would work for the older version, but for version above 2016b, this line works.
cellMatrix = {'A', 'B', 'C'; 'A', 'B', 'C';'A', 'B', 'C'};
if you want the first column,
firstCol = cellMatrix(:,1);
and so forth.
1 Comment
Abdulaziz Abutunis
on 16 Aug 2018
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!