MATLAB Answers

How to reshape every cell in a structure

11 views (last 30 days)
Blue
Blue on 15 Aug 2019
Commented: Blue on 15 Aug 2019
Hi,
I am trying to reshape every cell in a 1x4 structure with 10 fields. A simplified example would look like this but when I try to reshape every 1x48 cell into 4x12 cells I get the following error: Inputs to STRUCTFUN must be scalar structures.
b = struct('a', {[1:48], [1:48], [1:48], [1:48]})
structfun(@(a) reshape(a, 12, 4), b, 'UniformOutput', false)
Thoughts ?

  0 Comments

Sign in to comment.

Accepted Answer

madhan ravi
madhan ravi on 15 Aug 2019
arrayfun(@(x) reshape(b(x).a, 12, 4), 1:numel(b), 'UniformOutput', false)

  6 Comments

Show 3 older comments
Blue
Blue on 15 Aug 2019
Mmmm, something like this ?
b = struct('a', {[1:48], [1:48], [1:48], [1:48]})
fn = fieldnames(b);
for k = 1:length(fn)
j = char(fn(k))
b = arrayfun(@(x) reshape(b(x).j, 4, 12), 1:numel(b), 'UniformOutput', false)
end
madhan ravi
madhan ravi on 15 Aug 2019
fn = fieldnames(b);
for k = 1:numel(b)
for l = 1:numel(fn)
b(k).(fn{l})= reshape(b(k).(fn{l}), 4, 12);
end
end

Sign in to comment.

More Answers (0)

Sign in to answer this question.

Tags