Cody

# Problem 767. Assign numerical values to a structure with 1 field

Solution 529530

Submitted on 16 Nov 2014 by rifat
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
%% s = repmat(struct('prices', 0), 2, 3); data = [11 12 13 ;21 22 23 ]; % solution s2(1,1).prices = 11 s2(1,2).prices = 12 s2(1,3).prices = 13 s2(2,1).prices = 21 s2(2,2).prices = 22 s2(2,3).prices = 23 assert(isequal(assign_struct(s,data),s2)) % prevents cheating filetext = fileread('assign_struct.m') assert(isempty(strfind(filetext, 'for'))) assert(isempty(strfind(filetext, 'while')))

s2 = prices: 11 s2 = 1x2 struct array with fields: prices s2 = 1x3 struct array with fields: prices s2 = 2x3 struct array with fields: prices s2 = 2x3 struct array with fields: prices s2 = 2x3 struct array with fields: prices filetext = function s2= assign_struct(varargin) s = varargin{1}; data = varargin{2}; if length(varargin)>2 n = varargin{3}; end field = fieldnames(s); if length(varargin) > 2 s = setfield(s,{n},field{1},data(n)); if n < numel(data) s2 = assign_struct(s,data,n+1); else s2 = s; end else s2 = assign_struct(s,data,1); end end %This code written by profile_id 3437206

2   Pass
%% Another fieldname s = repmat(struct('yo', 0), 2, 3); data = [14 12 13 ;87 22 2.3 ]; % solution s2(1,1).yo = 14 s2(1,2).yo = 12 s2(1,3).yo = 13 s2(2,1).yo = 87 s2(2,2).yo = 22 s2(2,3).yo = 2.3 assert(isequal(assign_struct(s,data),s2)) % prevents cheating filetext = fileread('assign_struct.m') assert(isempty(strfind(filetext, 'for'))) assert(isempty(strfind(filetext, 'while')))

s2 = yo: 14 s2 = 1x2 struct array with fields: yo s2 = 1x3 struct array with fields: yo s2 = 2x3 struct array with fields: yo s2 = 2x3 struct array with fields: yo s2 = 2x3 struct array with fields: yo filetext = function s2= assign_struct(varargin) s = varargin{1}; data = varargin{2}; if length(varargin)>2 n = varargin{3}; end field = fieldnames(s); if length(varargin) > 2 s = setfield(s,{n},field{1},data(n)); if n < numel(data) s2 = assign_struct(s,data,n+1); else s2 = s; end else s2 = assign_struct(s,data,1); end end %This code written by profile_id 3437206

3   Pass
%% structure bigger with a random matrix s = repmat(struct('french', 0), 4, 4); data = rand(4); for i = 1:4 for j = 1:4 s2(i,j).french = data(i,j); end end assert(isequal(assign_struct(s,data),s2)) % prevents cheating filetext = fileread('assign_struct.m') assert(isempty(strfind(filetext, 'for'))) assert(isempty(strfind(filetext, 'while')))

filetext = function s2= assign_struct(varargin) s = varargin{1}; data = varargin{2}; if length(varargin)>2 n = varargin{3}; end field = fieldnames(s); if length(varargin) > 2 s = setfield(s,{n},field{1},data(n)); if n < numel(data) s2 = assign_struct(s,data,n+1); else s2 = s; end else s2 = assign_struct(s,data,1); end end %This code written by profile_id 3437206

### Community Treasure Hunt

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

Start Hunting!