VARSETS_LEN Returns the total number of variables in VARSETS NV = OM.VARSETS_LEN(VARSETS) Returns the total number of elements in the optimization sub-vector specified by VARSETS. See also VARSETS_CELL2STRUCT
0001 function nv = varsets_len(om, vs) 0002 %VARSETS_LEN Returns the total number of variables in VARSETS 0003 % NV = OM.VARSETS_LEN(VARSETS) 0004 % 0005 % Returns the total number of elements in the optimization sub-vector 0006 % specified by VARSETS. 0007 % 0008 % See also VARSETS_CELL2STRUCT 0009 0010 % MP-Opt-Model 0011 % Copyright (c) 2017-2020, Power Systems Engineering Research Center (PSERC) 0012 % by Ray Zimmerman, PSERC Cornell 0013 % 0014 % This file is part of MP-Opt-Model. 0015 % Covered by the 3-clause BSD License (see LICENSE file for details). 0016 % See https://github.com/MATPOWER/mp-opt-model for more info. 0017 0018 persistent sn; 0019 if isempty(vs) 0020 nv = om.var.N; 0021 else 0022 nv = 0; 0023 0024 %% calls to substruct() are relatively expensive, so we pre-build the 0025 %% struct for addressing numeric array fields, updating only 0026 %% the subscripts before use 0027 if isempty(sn) 0028 sn = struct('type', {'.', '()'}, 'subs', {'', 1}); 0029 end 0030 0031 for v = 1:length(vs) 0032 idx = vs(v).idx; 0033 if isempty(idx) 0034 N = om.var.idx.N.(vs(v).name); 0035 else 0036 % (calls to substruct() are relatively expensive ... 0037 % sn = substruct('.', vs(v).name, '()', vs(v).idx); 0038 % ... so replace it with these more efficient lines) 0039 sn(1).subs = vs(v).name; 0040 sn(2).subs = idx; 0041 N = subsref(om.var.idx.N, sn); 0042 end 0043 nv = nv + sum(N(:)); 0044 end 0045 end