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

4 次查看(过去 30 天)
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