how to access elements of a vector inside a cell array
조회 수: 3 (최근 30일)
이전 댓글 표시
I need to access with a for loop all the elements of SimulatedPoints but I struggle with finding the notation. This is what I did but it returns the entire vector of the cell.
for j=1:length(SimulatedPoints.Codes{idxRow,:})
end
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/709197/image.png)
댓글 수: 1
채택된 답변
Dave B
2021년 8월 11일
The length part should return the entire contents of the cell, so your code looks fine to me. Inside the loop you'd refer to the individual elements of the cell:
name=["a";"b"];
codes=[{rand(3,1);rand(5,1)}];
t=table(name,codes)
row = 2;
for i = 1:length(t.codes{row})
disp(t.codes{row}(i))
end
댓글 수: 2
Stephen23
2021년 8월 11일
"The length part should return the entire contents of the cell"
What cell array?
Is your answer finally an official TMW announcement that cell arrays andf tables really are one and the same?
Dave B
2021년 8월 11일
편집: Dave B
2021년 8월 11일
@Stephen Cobeldick - My answer is an offical TMW announcement of anything, I just jump on answers sometimes to help out when I'm not too busy. But personally, I certainly don't think of tables and cell arrays as the same, they seem quite different in many regards (although you can convert between them quite easily). I didn't mean to jump into something controversial.
In this example there's clearly a cell, you can see it in the disp, and isa(t.codes(2),'cell') returns true. So t.codes{row} returns the contents of the cell t.codes(row), right?
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Cell Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!