Cody

# Problem 42859. Convert nested struct-array to numeric array

Solution 1584664

Submitted on 16 Jul 2018
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

### Test Suite

Test Status Code Input and Output
1   Pass
data_ref_temp = rand(5,4,3,2); data.siz = size(data_ref_temp); global data for i1 = 1:data.siz(1) data.altitude.location(i1) = rand; for i2 = 1:data.siz(2) data.floor.location(i1).room(i2) = rand; for i3 = 1:data.siz(3) data.scale.location(i1).room(i2).device(i3) = rand; for i4 = 1:data.siz(4) data.temperature.location(i1).room(i2).device(i3).sample(i4) = data_ref_temp(i1,i2,i3,i4); data.timestamp.location(i1).room(i2).device(i3).sample(i4) = i4; end end end end assert(isequal(struct2mat(data.temperature),data_ref_temp))

[Warning: The value of local variables may have been changed to match the globals. Future versions of MATLAB will require that you declare a variable to be global before you use that variable.] [> In ScoringEngineTestPoint1 (line 3) In solutionTest (line 3)]

2   Fail
global data data_ref_time = repmat(shiftdim(1:data.siz(4),-2),data.siz(1:3)); assert(isequal(struct2mat(data.timestamp),data_ref_time))

Error using reshape Size arguments must be real integers. Error in struct2mat (line 9) mat = permute(reshape(s,n),length(n):-1:1); Error in struct2mat (line 6) mat = struct2mat([p{:}]); Error in struct2mat (line 6) mat = struct2mat([p{:}]); Error in struct2mat (line 6) mat = struct2mat([p{:}]); Error in struct2mat (line 6) mat = struct2mat([p{:}]); Error in Test2 (line 3) assert(isequal(struct2mat(data.timestamp),data_ref_time))

3   Fail
global data assert(isequal(struct2mat(data.altitude),data.altitude.location.'))

Error using reshape Size arguments must be real integers. Error in struct2mat (line 9) mat = permute(reshape(s,n),length(n):-1:1); Error in struct2mat (line 6) mat = struct2mat([p{:}]); Error in Test3 (line 2) assert(isequal(struct2mat(data.altitude),data.altitude.location.'))

4   Fail
global data assert(isequal(struct2mat(data.floor),cat(1,data.floor.location.room)))

Error using reshape Size arguments must be real integers. Error in struct2mat (line 9) mat = permute(reshape(s,n),length(n):-1:1); Error in struct2mat (line 6) mat = struct2mat([p{:}]); Error in struct2mat (line 6) mat = struct2mat([p{:}]); Error in Test4 (line 2) assert(isequal(struct2mat(data.floor),cat(1,data.floor.location.room)))

5   Fail
global data data.scale.location;[ans.room];[ans.device]; assert(isequal(struct2mat(data.scale),permute(reshape(ans,3,4,5),[3 2 1])))

Error using reshape Size arguments must be real integers. Error in struct2mat (line 9) mat = permute(reshape(s,n),length(n):-1:1); Error in struct2mat (line 6) mat = struct2mat([p{:}]); Error in struct2mat (line 6) mat = struct2mat([p{:}]); Error in struct2mat (line 6) mat = struct2mat([p{:}]); Error in Test5 (line 3) assert(isequal(struct2mat(data.scale),permute(reshape(ans,3,4,5),[3 2 1])))