How do I decrease a 1x1 structure with 92 fields to a 1x1 structure with 12 fields?

I have a 1x1 structure with 92 fields:
Temp =
TR0001: 22.4719
TR0002: 22.5238
TR0003: 22.3724
TR0004: 22.3784
TR0005: 22.4431
TR0006: 22.2697
TR0007: 22.3105
TR0101: 21.7538
TR0102: 21.7433
TR0103: 21.7190
TR0104: 21.6919
TR0105: 21.6767
TR0106: 21.6614
TR0107: 21.6862
TR0201: 21.8343
TR0202: 21.8429
TR0203: 21.8305
TR0204: 21.7957
TR0205: 21.7590
TR0206: 21.7745
TR0207: 21.7829
TR0301: 21.3286
TR0302: 21.3371
TR0303: 21.8619
TR0304: 21.8719
TR0305: 21.8800
TR0306: 21.7624
TR0307: 21.4176
TR0400: 21.3474
TR0401: 21.3814
TR0402: 21.3814
TR0403: 21.3429
TR0404: 21.3557
TR0405: 21.3686
TR0406: 21.3848
TR0407: 21.3557
TR0500: 21.3712
TR0501: 21.5105
TR0502: 21.4405
TR0503: 21.3800
TR0504: 21.3924
TR0505: 21.3962
TR0506: 21.3833
TR0507: 21.3881
TR0600: 21.3565
TR0601: 21.3348
TR0602: 21.3405
TR0603: 21.3348
TR0604: 21.3205
TR0605: 21.3238
TR0606: 21.3276
TR0607: 21.3267
TR0700: 21.3561
TR0701: 21.4352
TR0702: 21.4429
TR0703: 21.3462
TR0704: 21.3652
TR0705: 21.3776
TR0706: 21.3938
TR0707: 21.4095
TR0800: 21.4452
TR0801: 21.5305
TR0802: 21.5476
TR0803: 21.4595
TR0804: 21.4800
TR0805: 21.4910
TR0806: 21.5024
TR0807: 21.5114
TR0900: 21.7455
TR0901: 21.7490
TR0902: 21.7510
TR0903: 21.8400
TR0904: 21.7676
TR0905: 21.7600
TR0906: 21.7500
TR0907: 21.7505
TR1000: 21.9639
TR1001: 21.7924
TR1002: 21.7952
TR1003: 21.8276
TR1004: 21.8076
TR1005: 21.8057
TR1006: 21.8014
TR1007: 21.7962
TR1800: 21.7456
TR1801: 21.7729
TR1802: 21.7600
TR1803: 21.7952
TR1804: 21.7610
TR1805: 21.7495
TR1806: 21.7571
TR1807: 21.7543
I have grouped them by, e.g.:
TAvg.TR00XX (:,1) = [Temp.TR0001,Temp.TR0002,Temp.TR0003,Temp.TR0004,Temp.TR0005,Temp.TR0006,Temp.TR0007];
TAvg.TR01XX (:,1) = [Temp.TR0101,Temp.TR0102,Temp.TR0103,Temp.TR0104,Temp.TR0105,Temp.TR0106,Temp.TR0107];
.
.
TAvg.TR18XX (:,1) = [Temp.TR1801,Temp.TR1802,Temp.TR1803,Temp.TR1804,Temp.TR1805,Temp.TR1806,Temp.TR1807];
which results in a 1x1 structure with 12 fields:
TAvg =
TR00XX: [7x1 double]
TR01XX: [7x1 double]
TR02XX: [7x1 double]
TR03XX: [7x1 double]
TR04XX: [7x1 double]
TR05XX: [7x1 double]
TR06XX: [7x1 double]
TR07XX: [7x1 double]
TR08XX: [7x1 double]
TR09XX: [7x1 double]
TR10XX: [7x1 double]
TR18XX: [7x1 double]
Could there be a more direct approach to obtain the 1x1 structure with 12 fields?

댓글 수: 2

Creating numbered variables is a great way to cause many future problems. Welcome to the world you created.
Learn to use indexing. Don't create numbered variables, even if they are just fields in a struct.
You can access fields of a struct programmatically, just as you probably created them. Better is to have avoided the problem in the first place. You had a vector of numbers, so why not have created a vector to contain them??????
"Could there be a more direct approach"
Yes, don't store lots of individual fields. Storing these data in one array would make processing much simpler. You can also store the meta-data as well, if you wish.

답변 (1개)

Of course! Computers are very good at doing repetitive tasks as you have here:
fn = fieldnames(Temp); %get list of fields of original structure
fieldids = str2double(cellfun(@(f) f(3:4), fn, 'UniformOutput', false)); %extract value of char 3 and 4 of each field
[groupid, ~, subs] = unique(fieldids);
fieldvalues = struct2cell(Temp);
Tavg = struct();
for group = 1:numel(groupid);
Tavg.(sprintf('TR%02dXX', groupid(group))) = [fieldvalues{group == subs}];
end

이 질문은 마감되었습니다.

질문:

2016년 9월 22일

마감:

2021년 8월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by