title: MATLAB adding fields to a struct in a function
body: <p>My structure has three fields:</p>
  
  <pre><code>&gt;&gt; design
  
  design = 
  
                  E: [1x101 double]
                  F: [1x21 double]
           bandsImg: []
  </code></pre>
  
  <p>Now, I modify that structure in a function by adding a new field:</p>
  
  <pre><code>function design = loadSelfSimp(design,values)
  design.newField = values(1);
  </code></pre>
  
  <p>And finally in my main code:</p>
  
  <pre><code>design = loadSelfSimp(design, somevalues)
  </code></pre>
  
  <p>MATLAB complains about:</p>
  
  <pre><code>Subscripted assignment between dissimilar structures.
  
  Error in selfSimpPoor (line 101)
          design(i) = loadSelfSimp(design(i),selfSimp_out);
  </code></pre>
  
  <p>What is the problem here? How to easily fix it?</p>
  
  <p>The code:</p>
  
  <pre><code>    function [design,conv] = selfSimp(design,question)
  
  if nargin == 1
      question = 1; 
  end
  numDes = length(design);
  
  for i = 1:numDes
  
      well = design(i).well;
      barr = design(i).barr;
      dopeWell = design(i).dopeWell;
      dopeBarr = design(i).dopeBarr;
      barrX = design(i).barrX;
      elemDens = design(i).elemDens;
      numPer = design(i).numPer;
      iterFac = design(i).iterFac;
      posMeth = design(i).posMeth;
      if isfield(design(i),'bandSwitch')
          bandSwitch = design(i).bandSwitch;
          design(i).wvFunslh = [];
          design(i).Elh = [];
          design(i).wvFunshh = [];
          design(i).Ehh = [];
      else
          bandSwitch = 1;
      end
      elecTemp = design(i).elecTemp;
  
      numWell = length(well);
  
      %check if there is any value in minSplitField
      if isfield(design(i),'minSplitField') &amp;&amp; (isempty(design(i).minSplitField) == 0)
          minSplitField = design(i).minSplitField;
      else
          F = design(i).F;
          minSplitField = F(ceil(numF/2));
      end
  
      %%%%% do some tests before starting
      %%% Test to see  if F, well and barr are row vectors
  
  
      sze = size(well);
      if sze(1)~=1
          disp('        WARNING: well is either not one dimentional or is a column vector')
      end
      sze = size(barr);
      if sze(1)~=1
          disp('        WARNING: barr is either not one dimentional or is a column vector')
      end
      clear sze
  
      disp(minSplitField);
  
      %save the values found for read in by 'qclsolve'
      fid = fopen('../../bin/qclsolve_in.dat','w');
      fprintf(fid,'#\n#\n# Number of wells:\n');
      fprintf(fid,'%-2.0f',numWell);
      fprintf(fid,'\n#\n#\n#\n# Widths of wells:\n');
      fprintf(fid,'%-9.4f',well);
      fprintf(fid,'\n#\n#\n#\n# Widths of barriers\n');
      fprintf(fid,'%-9.4f',barr);
      fprintf(fid,'\n#\n#\n#\n# Doping in wells\n');
      fprintf(fid,'%-10.2e',dopeWell);
      fprintf(fid,'\n#\n#\n#\n# Doping barriers\n');
      fprintf(fid,'%-10.2e',dopeBarr);
      fprintf(fid,'\n#\n# Aluminum barrier fraction\n');
      fprintf(fid,'%-6.4f',barrX);
      fprintf(fid,'\n#\n# Element density (for grid/mesh)\n');
      fprintf(fid,'%-4.2f',elemDens);
      fprintf(fid,'\n#\n# minSplitField - field at which minimum injector-upper occours\n');
      fprintf(fid,'%-4.2f',minSplitField);
      fprintf(fid,'\n#\n# number of period repeats\n');
      fprintf(fid,'%-2.0f',numPer);
      fprintf(fid,'\n#\n# starting fraction of electrons to use in self-consistant\n');
      fprintf(fid,'%-4.2f',iterFac);
      fprintf(fid,'\n#\n# method of finding electron positions (1 or 2, 1 is best)\n');
      fprintf(fid,'%-2.0f',posMeth);
      fprintf(fid,'\n#\n# Which band we are soling in (1=Conduction, 2=Valence)\n');
      fprintf(fid,'%-2.0f',bandSwitch);
      fprintf(fid,'\n#\n# If the solution is to self-consistant then =1, else 0 \n');
      fprintf(fid,'%-2.0f',1);
      fprintf(fid,'\n#\n# Electron temperature in Kelvin\n');
      fprintf(fid,'%-6.2f',elecTemp);
      fclose(fid);
  
      %call the solver to print the band structure at the minimum splitting field
      if isunix
          unix('././bin/qclsolve');
      else
          dos('..\..\bin\qclsolve.exe');
      end
  
      selfSimp_out = load('../../bin/states_out.dat');
  
      conv(i) = selfSimp_out(1,end);
  
      %what to do depends on if it converged
      if conv(i) || (question==2)
          design(i) = loadSelfSimp(design(i),selfSimp_out);
  % design_a = loadSelfSimp(design(i),selfSimp_out);
      elseif question == 1
          cont = input('Did not converge, load anyway? (y/n)','s');
          if cont == 'y'
              design(i) = loadSelfSimp(design(i),selfSimp_out);
          end
      end
  
  end
  
  function design = loadSelfSimp(design,selfSimp_out)
  
  lower = design.lowInjUp(1);
  injector = design.lowInjUp(2);
  upper = design.lowInjUp(3);
  upper2 = design.lowInjUp(4);
  extractor = design.lowInjUp(5);
  LO = design.lowInjUp(6); % extractor1
  LO1 = design.lowInjUp(7);%LO
  
  
  
  try
      bandSwitch = design.bandSwitch;
  catch
      bandSwitch = 0;
  end
  
  E = selfSimp_out(end,3:end);
  %E = selfSimp_out(45,3:45);
  wvFuns = selfSimp_out(1:(end-1),:);
  
  %find the frequency and dipoles
  frequency = (1.60219e-19/6.6262e-34)*(E(upper) - E(lower));
  
  % find voltage/period
  Vpp = design.minSplitField*(design.well(1)+design.well(2)+design.well(3)+design.well(4)+ design.barr(1)+design.barr(2)+design.barr(3)+design.barr(4));
  
  %Find rabi frequency
  hbar = 6.58211928e-16;          % in terms of eV
  q = 1.6022e-19;
  
  
  minSplitting = E(upper) - E(injector);% resonance of energy levels in both the wells 
  minSplitting2 = E(extractor) - E(lower);
  
  design.E = E;
  
  z1_simp_iu = dipole(design,upper,injector)
  
  rabi_freq_iu = abs(design.minSplitField*z1_simp_iu*(1e-5)/(hbar));
  
  coupling_energy_iu = hbar*rabi_freq_iu*10^3;
  
  design.coupling_energy_iu = coupling_energy_iu;
  </code></pre>
  
  <p>And my structure:</p>
  
  <pre><code>c1 = 
  
                  E: [1x101 double]
                  F: [1x21 double]
           bandsImg: []
               barr: [6 3.8000 1.8000 3.3000 5.2000]
              barrX: 0.1500
            dipole1: 3.5718
           dipole1V: [71x1 double]
            dipole2: 0.2655
           dipole2V: [71x1 double]
           dopeBarr: [0 0 0 0 0]
           dopeWell: [0 0 0 0 1.9600e+16]
           elemDens: 2
          frequency: 3.7630e+12
            iterFac: 1
           lowInjUp: [11 16 14 13 10 9 7]
      minSplitField: -7.3500
       minSplitting: -0.0019
               name: ' Modified Amanti deisgn ETH'
             numPer: 3
            posMeth: 2
         splittingV: [1x20 double]
               well: [12 9.5000 9.5000 9 19.5000]
             wvFuns: [542x103 double]
              alpha: [4x1001 double]
            dipoles: []
           elecTemp: 100
      minSplitting2: []
        splittingV2: []
            dipole3: []
  </code></pre>
  
  <p><code>design.E = E;</code> doesn't cause problems (design.E already exists), but <code>design.coupling_energy_iu = coupling_energy_iu;</code> does.</p>