Shorten description of multiple fields and values in struct

2 次查看(过去 30 天)
Hi,
does anyone have a method with a loop or other to shorten the description of multiple fields and values in struct?
=struct(Varnames{1},VarDec(:,:,1),Varnames{2},VarDec(:,:,2),Varnames{3},VarDec(:,:,3),Varnames{4},VarDec(:,:,4),Varnames{5},VarDec(:,:,5),Varnames{6},VarDec(:,:,6),Varnames{7},VarDec(:,:,7),Varnames{8},VarDec(:,:,8),Varnames{9},VarDec(:,:,9),Varnames{10},VarDec(:,:,10),Varnames{11},VarDec(:,:,11),Varnames{12},VarDec(:,:,12),Varnames{13},VarDec(:,:,13),Varnames{14},VarDec(:,:,14),Varnames{15},VarDec(:,:,15),Varnames{16},VarDec(:,:,16),Varnames{17},VarDec(:,:,17),Varnames{18},VarDec(:,:,18));
Thank you
Arnaud

采纳的回答

Stephen23
Stephen23 2021-11-3
编辑:Stephen23 2021-11-3
Either use CELL2STRUCT:
% Fake data:
Varnames = cellstr("X"+(1:18));
VarDec = randi(9,5,3,18);
% Convert to struct:
C = num2cell(VarDec,1:2);
S = cell2struct(C,Varnames,3)
Or a simple comma-separated list with STRUCT:
D = reshape(Varnames,1,[]);
D(2,:) = C;
S = struct(D{:})

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by