Data Entry with struct

19 views (last 30 days)
Shahrear Khan Faisal
Shahrear Khan Faisal on 24 Sep 2020
Answered: Black Woods on 17 Dec 2022
I'm stuck with the following data entry assignment. Your suggestions will be highly appreciated.
In a voting center, the voters are required to give their names and ID numbers to the voting staff before going int incoming voters' information. Write a function called voters to record the incoming voters' information. The function should take an unspecified number of inputs, but the first input will be the current database. The rest of the arguments must come order of name, a string or char array, and ID, an integer or integer-valued double. If there is at least one ocurrence of no ID number after a name, or the data types are not what's required, return the original database. The function should return a struct array containing Name (a string) and ID( a double) fields as shown below
>> database = voters([], 'Brandon', 12356)
database =
struct with fields:
Name: "Brandon"
ID: 12356
>> database = voters(database, "Mike", 9876, 'Akos', 112233)
database =
1x3 struct array with fields:
Name
ID
>> database(end)
ans =
struct with fields:
Name: "Akos"
ID: 112233
However, if there is an illegal call, the database should not change:
>> database
database =
1x3 struct array with fields:
Name
ID
>> database = voters(database, 'Student', 99999, "No ID")
database =
1x3 struct array with fields:
Name
ID
Here's the code I tried -
function database = voters(varargin)
database = struct('Name', {}, 'ID', {});
if nargin >= 5 & mod(nargin, 2) ~= 0
for i = 2:2:nargin-1
if ischar(varargin{i}) & isa(varargin{i+1}, 'double') & isempty(varargin{1})
database = struct('Name', varargin{i}, 'ID', varargin{i+1})];
else
database = database;
break;
end
end
else if nargin == 3 & ischar(varargin{2}) & isa(varargin{3}, 'double') & isempty(varargin{1})
database = struct('Name', varargin{2}, 'ID', varargin{3});
else
database = database;
end
end
  1 Comment
likith yerram
likith yerram on 12 Oct 2020
Error in solution: Line: 6 Column: 34
Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters.

Sign in to comment.

Accepted Answer

Alberto Zekry
Alberto Zekry on 14 Oct 2020
function database = voters(di,varargin)
if isempty(di)
database = struct('Name', {}, 'ID', {});
end
if rem(nargin,2)==0
database=di
return
end
si=length(varargin)/2;
for ii = 1:si
jj=2*ii;
if (ischar(varargin{jj-1}) || isstring(varargin{jj-1})) && isa(varargin{jj}, 'double') && (fix(varargin{jj})==(varargin{jj}))
database(ii) = struct('Name', convertCharsToStrings(varargin{(2*ii)-1}), 'ID', double(varargin{2*ii}));
else
database=di
return
end
end
database=[di database]
end

More Answers (3)

Ahsan Ali
Ahsan Ali on 30 Oct 2020
Check this Out (Tested)
function database = voters(di,varargin)
if isempty(di)
database = struct('Name', {}, 'ID', {});
end
if rem(nargin,2)==0
database=di;
return
end
si=length(varargin)/2;
for ii = 1:si
jj=2*ii;
if (ischar(varargin{jj-1}) || isstring(varargin{jj-1})) && isa(varargin{jj}, 'double') && (fix(varargin{jj})==(varargin{jj}))
database(ii) = struct('Name', string(varargin{(2*ii)-1}), 'ID', double(varargin{2*ii}));
else
database=di;
return
end
end
database=[di database];
end

Abdul Quadir Khan
Abdul Quadir Khan on 7 Nov 2020
function database = voters(di,varargin)
if isempty(di)
database = struct('Name', {}, 'ID', {});
end
if rem(nargin,2)==0
database=di
return
end
si=length(varargin)/2;
for ii = 1:si
jj=2*ii;
if (ischar(varargin{jj-1}) || isstring(varargin{jj-1})) && isa(varargin{jj}, 'double') && (fix(varargin{jj})==(varargin{jj}))
database(ii) = struct('Name', convertCharsToStrings(varargin{(2*ii)-1}), 'ID', double(varargin{2*ii}));
else
database=di
return
end
end
database=[di database]
end
  1 Comment
PADMAKAR
PADMAKAR on 23 Mar 2021
I am tryint to use setfield to add fields to a structure. It does not seem to work. The problem is the same as above. Here is my code
function [dbase_out]=voters(dbase_in,varargin)
if isempty(dbase_in)
dbase_out = struct('Name', {}, 'ID', {});
return
end
i
f isempty(varargin)
dbase_out=struct('Name',{},'ID',{});
dbase_out=dbase_in;
return
elseif ~rem(length(varargin),2)==0
dbase_out=struct('Name',{},'ID',{});
dbase_out=dbase_in;
return
end
%----------------------------------------------------------------------------------------------------------------------------------------------
for i = 1:2:length(varargin)/2
if ischar(varargin{i}) || isstring(varargin{i})
else
dbase_out = dbase_in;
return;
end
end
% create a empty structure because the number of fields required also includes empty fields from database
dbase_out = struct('Name', {}, 'ID', {});
for ii=1:length(varargin)/2
n=2*ii ;
if isa(varargin{n}, 'double') && (fix(varargin{n})==(varargin{n}))
dname{ii}= string(varargin{n-1});
dID{ii}= double(varargin{n});
end
end
%dbase_out=struct('Name',dname,'ID',dID);
for i=2: length(varargin)/2
dbase_out=setfield(dbase_out,'Name',dname(i));
dbase_out=setfield(dbase_out,'ID',dID(i));
end

Sign in to comment.


Black Woods
Black Woods on 17 Dec 2022
function database=voters(varargin)
if isempty(varargin{1})
jj=ones(1,1);
else
jj=length(varargin{1})+1;database=varargin{1};
end
if isequal(mod(length(varargin)-1,2),0)
for ii=2:2:length(varargin)
if (isa(varargin{ii},'char')||isa(varargin{ii},'string'))&&((isa(varargin{ii+1},'double')||isa(varargin{ii+1},'int32'))&&isequal(fix(varargin{ii+1}),varargin{ii+1}))
database(jj).Name=string(varargin{ii});database(jj).ID=varargin{ii+1};jj=jj+1;
else
database=varargin{1};
return
end
end
else
database=varargin{1};
return
end

Tags

Products

Community Treasure Hunt

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

Start Hunting!