Shorten description of multiple fields and values in struct

1 view (last 30 days)
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

Accepted Answer

Stephen23
Stephen23 on 3 Nov 2021
Edited: Stephen23 on 3 Nov 2021
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{:})

More Answers (0)

Categories

Find more on Structures in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!