% Last edited on 2024-09-23 17:25:10 by stolfi

function jv = keg_defvar(Mval, val, Mname, name, Mfmtn, fmtn, Mfmtv, fmtv, Msel, sel)

  % The {Mval} parameter must be a natrix; let {NI} and {NJ} be the
  % count of rows and columns, respectively. The {val} parameter must be
  % a column vector with {NI} elements, which is appended a new column
  % {NJ+1} of {Mval}.
  % 
  % Each of the parameters {Mname,Mfmtn,Mfmtv} must be an array of
  % characters with {NJ} rows and {name,fmtn,fmtv} must
  % be strings, which are appended as row {NJ+1} of those arrays. The string 
  % {Mname(j,:)} is the name of column {j} of {Mval}. The strings
  % {Mfmtn(j,:)} and {Mfmtv(:j)} are format codes used to print the name
  % {Mname(j,:)} and the values in {Mval(j,:)}, respectively.
  %  
  % The paraneter {Msel} must be a 1 by {NJ} vector of logicals  It gets 
  % expanded by appending the new element {sel}, which must be a logical.
  
  NI, NJ = size(Mval);
  keg_check_size(val,NI,NJ,"val");
  jv = NJ+1;
  Mval(:,jv) = val;
  
  keg_check_size(Mname,NJ,-1,"Mname");
  keg_check_size(name,1,-1,"name");
  Mname(jv,:) = sprintf("%12s", name);
  
  keg_check_size(Mfmtn,NJ,-1,"Mfmtn");
  keg_check_size(fmtn,1,-1,"fmtn");
  Mfmtn(jv,:) = sprintf("%12s", fmtn);
  
  keg_check_size(Mfmtv,NJ,-1,"Mfmtv");
  keg_check_size(fmtv,1,-1,"fmtv");
  Mfmtv(jv,:) = sprintf("%12s", fmtv);
  
  keg_check_size(Msel,1,NJ,"Msel");
  Msel(:,jv) = sel;
endfunction  
  
