MATLAB将字段添加到函数中的结构中

时间:2014-05-14 15:00:39

标签: matlab structure

我的结构有三个字段:

>> design

design = 

                E: [1x101 double]
                F: [1x21 double]
         bandsImg: []

现在,我通过添加一个新字段来修改函数中的结构:

function design = loadSelfSimp(design,values)
design.newField = values(1);

最后在我的主要代码中:

design = loadSelfSimp(design, somevalues)

MATLAB抱怨:

Subscripted assignment between dissimilar structures.

Error in selfSimpPoor (line 101)
        design(i) = loadSelfSimp(design(i),selfSimp_out);

这是什么问题?如何轻松解决?

代码:

    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') && (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;

我的结构:

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: []

design.E = E;不会导致问题(design.E已存在),但design.coupling_energy_iu = coupling_energy_iu;会出现问题。

1 个答案:

答案 0 :(得分:2)

当您尝试为结构数组分配新元素时,会导致该问题。如果此元素具有不同的字段集,则会给出指定的错误,例如:

for k = 1:4;
    s(k).a = k;
    s(k).b = k;
end
t.a = 5;
try
    s(5) = t;
catch
    fprintf('This is error1\n');
end
t.b = 5;
t.c = 5;
try
    s(5) = t;
catch
    fprintf('This is error2\n');
end

% Possible cure
s(1).c = [];
s(5) = t;
% t.b=[]; s(5) = t;
fprintf('This works!\n');

但是,在您的代码中,您可能会进行某种检查以确保所有字段都在那里。另一种解决方案是使用单元阵列。