Extract structure array elements into seperate matrix
조회 수: 1 (최근 30일)
이전 댓글 표시
Bob Thompson
2018년 4월 28일
답변: Kaushik Lakshminarasimhan
2018년 4월 28일
I have a structure array with two fields that has multiple sets of information:
data(1).setnum = 1; % Contains integers to count set numbers
data(1).datablock = [1 2 3; 4 5 6; 7 8 9]; % Contains a matrix of numeric elements.
data(2).setnum = 2;
data(2).datablock = [1 2 3; 4 5 6; 7 8 9];
I would like to extract a specific element from each block of data to form a new matrix. For example I would like the element (1,2) from each datablock so that my new matrix becomes the following.
extracted = [2, 2];
I was attempting to use the line following but run into an error.
extracted = [data.datablock(1,2)];
Expected one output from a curly brace or dot indexing expression, but there were 2 results.
I found the following article, which basically says that what I'm trying to do is impossible by this method, but the article is two years old, so I'm looking to confirm that this method is still not possible, or if I have made some kind of syntax error.
댓글 수: 0
채택된 답변
Kaushik Lakshminarasimhan
2018년 4월 28일
No, that syntax won't work. If you want a one-liner, here's one alternative:
extracted = cellfun(@(x) x(1,2), {data.datablock});
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!