any simplified way to get the length of each variable in structure

조회 수: 4 (최근 30일)
Yu Li
Yu Li 2019년 4월 27일
댓글: Yu Li 2019년 4월 27일
Hi:
I have a structure, each element of the structure has a different size, which is what I neede.
it is very easy to write a for-loop to get it, but I want know if there is any easier way?
Please see attached test file, and below is the test code:
for i=1:1:length(test)
sz(i)=length(test(i).a);
end
Bests,
Yu

채택된 답변

Matt J
Matt J 2019년 4월 27일
sz = arrayfun(@(t) length(t.a), test);

추가 답변 (1개)

madhan ravi
madhan ravi 2019년 4월 27일
arrayfun(@(x)length(x.a),test) % use numel if the variables are a vector , I didn’t download your file so not sure how your variables are but this seems to do what you want.

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by