Home > matpower4.1 > savecase.m

savecase

PURPOSE ^

SAVECASE Saves a MATPOWER case file, given a filename and the data.

SYNOPSIS ^

function fname_out = savecase(fname, varargin)

DESCRIPTION ^

SAVECASE  Saves a MATPOWER case file, given a filename and the data.
   SAVECASE(FNAME, CASESTRUCT)
   SAVECASE(FNAME, CASESTRUCT, VERSION)
   SAVECASE(FNAME, BASEMVA, BUS, GEN, BRANCH)
   SAVECASE(FNAME, BASEMVA, BUS, GEN, BRANCH, GENCOST)
   SAVECASE(FNAME, BASEMVA, BUS, GEN, BRANCH, AREAS, GENCOST)
   SAVECASE(FNAME, COMMENT, CASESTRUCT)
   SAVECASE(FNAME, COMMENT, CASESTRUCT, VERSION)
   SAVECASE(FNAME, COMMENT, BASEMVA, BUS, GEN, BRANCH)
   SAVECASE(FNAME, COMMENT, BASEMVA, BUS, GEN, BRANCH, GENCOST)
   SAVECASE(FNAME, COMMENT, BASEMVA, BUS, GEN, BRANCH, AREAS, GENCOST)

   FNAME = SAVECASE(FNAME, ...)

   Writes a MATPOWER case file, given a filename and data struct or list of
   data matrices. The FNAME parameter is the name of the file to be created or
   overwritten. If FNAME ends with '.mat' it saves the case as a MAT-file
   otherwise it saves it as an M-file. Optionally returns the filename,
   with extension added if necessary. The optional COMMENT argument is
   either string (single line comment) or a cell array of strings which
   are inserted as comments. When using a MATPOWER case struct, if the
   optional VERSION argument is '1' it will modify the data matrices to
   version 1 format before saving.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function fname_out = savecase(fname, varargin)
0002 %SAVECASE  Saves a MATPOWER case file, given a filename and the data.
0003 %   SAVECASE(FNAME, CASESTRUCT)
0004 %   SAVECASE(FNAME, CASESTRUCT, VERSION)
0005 %   SAVECASE(FNAME, BASEMVA, BUS, GEN, BRANCH)
0006 %   SAVECASE(FNAME, BASEMVA, BUS, GEN, BRANCH, GENCOST)
0007 %   SAVECASE(FNAME, BASEMVA, BUS, GEN, BRANCH, AREAS, GENCOST)
0008 %   SAVECASE(FNAME, COMMENT, CASESTRUCT)
0009 %   SAVECASE(FNAME, COMMENT, CASESTRUCT, VERSION)
0010 %   SAVECASE(FNAME, COMMENT, BASEMVA, BUS, GEN, BRANCH)
0011 %   SAVECASE(FNAME, COMMENT, BASEMVA, BUS, GEN, BRANCH, GENCOST)
0012 %   SAVECASE(FNAME, COMMENT, BASEMVA, BUS, GEN, BRANCH, AREAS, GENCOST)
0013 %
0014 %   FNAME = SAVECASE(FNAME, ...)
0015 %
0016 %   Writes a MATPOWER case file, given a filename and data struct or list of
0017 %   data matrices. The FNAME parameter is the name of the file to be created or
0018 %   overwritten. If FNAME ends with '.mat' it saves the case as a MAT-file
0019 %   otherwise it saves it as an M-file. Optionally returns the filename,
0020 %   with extension added if necessary. The optional COMMENT argument is
0021 %   either string (single line comment) or a cell array of strings which
0022 %   are inserted as comments. When using a MATPOWER case struct, if the
0023 %   optional VERSION argument is '1' it will modify the data matrices to
0024 %   version 1 format before saving.
0025 
0026 %   MATPOWER
0027 %   $Id: savecase.m,v 1.36 2011/09/28 20:10:23 cvs Exp $
0028 %   by Carlos E. Murillo-Sanchez, PSERC Cornell & Universidad Autonoma de Manizales
0029 %   and Ray Zimmerman, PSERC Cornell
0030 %   Copyright (c) 1996-2010 by Power System Engineering Research Center (PSERC)
0031 %
0032 %   This file is part of MATPOWER.
0033 %   See http://www.pserc.cornell.edu/matpower/ for more info.
0034 %
0035 %   MATPOWER is free software: you can redistribute it and/or modify
0036 %   it under the terms of the GNU General Public License as published
0037 %   by the Free Software Foundation, either version 3 of the License,
0038 %   or (at your option) any later version.
0039 %
0040 %   MATPOWER is distributed in the hope that it will be useful,
0041 %   but WITHOUT ANY WARRANTY; without even the implied warranty of
0042 %   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0043 %   GNU General Public License for more details.
0044 %
0045 %   You should have received a copy of the GNU General Public License
0046 %   along with MATPOWER. If not, see <http://www.gnu.org/licenses/>.
0047 %
0048 %   Additional permission under GNU GPL version 3 section 7
0049 %
0050 %   If you modify MATPOWER, or any covered work, to interface with
0051 %   other modules (such as MATLAB code and MEX-files) available in a
0052 %   MATLAB(R) or comparable environment containing parts covered
0053 %   under other licensing terms, the licensors of MATPOWER grant
0054 %   you additional permission to convey the resulting work.
0055 
0056 %% define named indices into bus, gen, branch matrices
0057 [PQ, PV, REF, NONE, BUS_I, BUS_TYPE, PD, QD, GS, BS, BUS_AREA, VM, ...
0058     VA, BASE_KV, ZONE, VMAX, VMIN, LAM_P, LAM_Q, MU_VMAX, MU_VMIN] = idx_bus;
0059 [GEN_BUS, PG, QG, QMAX, QMIN, VG, MBASE, GEN_STATUS, PMAX, PMIN, ...
0060     MU_PMAX, MU_PMIN, MU_QMAX, MU_QMIN, PC1, PC2, QC1MIN, QC1MAX, ...
0061     QC2MIN, QC2MAX, RAMP_AGC, RAMP_10, RAMP_30, RAMP_Q, APF] = idx_gen;
0062 [F_BUS, T_BUS, BR_R, BR_X, BR_B, RATE_A, RATE_B, RATE_C, ...
0063     TAP, SHIFT, BR_STATUS, PF, QF, PT, QT, MU_SF, MU_ST, ...
0064     ANGMIN, ANGMAX, MU_ANGMIN, MU_ANGMAX] = idx_brch;
0065 [AREA_I, PRICE_REF_BUS] = idx_area;
0066 [PW_LINEAR, POLYNOMIAL, MODEL, STARTUP, SHUTDOWN, NCOST, COST] = idx_cost;
0067 
0068 %% default arguments
0069 if ischar(varargin{1}) || iscell(varargin{1})
0070     comment = varargin{1};
0071     [args{1:(length(varargin)-1)}] = deal(varargin{2:end});
0072 else
0073     comment = '';
0074     args = varargin;
0075 end
0076 mpc_ver = '2';              %% default MATPOWER case file version
0077 if isstruct(args{1})        %% 1st real argument is a struct
0078     mpc = args{1};
0079     if length(args) > 1
0080         mpc.version = args{2};
0081         mpc_ver = mpc.version;
0082     end
0083     baseMVA = mpc.baseMVA;
0084     bus     = mpc.bus;
0085     gen     = mpc.gen;
0086     branch  = mpc.branch;
0087     if isfield(mpc, 'areas')
0088         areas   = mpc.areas;
0089     end
0090     if isfield(mpc, 'gencost')
0091         gencost = mpc.gencost;
0092     end
0093 else                        %% 1st real argument is NOT a struct
0094     baseMVA = args{1};
0095     bus     = args{2};
0096     gen     = args{3};
0097     branch  = args{4};
0098     mpc.baseMVA = baseMVA;
0099     mpc.bus     = bus;
0100     mpc.gen     = gen;
0101     mpc.branch  = branch;
0102     if length(args) == 5
0103         gencost = args{5};
0104         mpc.gencost = gencost;
0105     end
0106     if length(args) == 6
0107         areas   = args{5};
0108         gencost = args{6};
0109         mpc.areas   = areas;
0110         mpc.gencost = gencost;
0111     end
0112 end
0113 
0114 %% modifications for version 1 format
0115 if strcmp(mpc_ver, '1')
0116     %% remove extra columns of gen
0117     if size(gen, 2) >= MU_QMIN
0118         gen = gen(:, [1:PMIN, MU_PMAX:MU_QMIN]);
0119     else
0120         gen = gen(:, 1:PMIN);
0121     end
0122     %% use the version 1 values for column names
0123     shift = MU_PMAX - PMIN - 1;
0124     tmp = num2cell([MU_PMAX, MU_PMIN, MU_QMAX, MU_QMIN] - shift);
0125     [MU_PMAX, MU_PMIN, MU_QMAX, MU_QMIN] = deal(tmp{:});
0126     
0127     %% remove extra columns of branch
0128     if size(branch, 2) >= MU_ST
0129         branch = branch(:, [1:BR_STATUS, PF:MU_ST]);
0130     elseif size(branch, 2) >= QT
0131         branch = branch(:, [1:BR_STATUS, PF:QT]);
0132     else
0133         branch = branch(:, 1:BR_STATUS);
0134     end
0135     %% use the version 1 values for column names
0136     shift = PF - BR_STATUS - 1;
0137     tmp = num2cell([PF, QF, PT, QT, MU_SF, MU_ST] - shift);
0138     [PF, QF, PT, QT, MU_SF, MU_ST] = deal(tmp{:});
0139 end
0140 
0141 %% verify valid filename
0142 l = length(fname);
0143 rootname = [];
0144 if l > 2
0145     if strcmp(fname(l-1:l), '.m')
0146         rootname = fname(1:l-2);
0147         extension = '.m';
0148     elseif l > 4
0149         if strcmp(fname(l-3:l), '.mat')
0150             rootname = fname(1:l-4);
0151             extension = '.mat';
0152         end
0153     end
0154 end
0155 if isempty(rootname)
0156     rootname = fname;
0157     extension = '.m';
0158     fname = [rootname, extension];
0159 end
0160 
0161 %% open and write the file
0162 if strcmp(extension, '.mat')        %% MAT-file
0163     vflag = '';
0164     if str2double(version('-release')) > 13
0165         vflag = ' -V6';
0166     end
0167     if strcmp(mpc_ver, '1')
0168         if exist('areas', 'var') && exist('gencost', 'var')
0169             cmd = sprintf('save %s baseMVA bus gen branch areas gencost%s;', rootname, vflag); 
0170         else
0171             cmd = sprintf('save %s baseMVA bus gen branch%s;', rootname, vflag); 
0172         end
0173     else
0174         cmd = sprintf('save %s mpc%s;', rootname, vflag); 
0175     end
0176     eval(cmd);
0177 else                                %% M-file
0178     %% open file
0179     [fd, msg] = fopen(fname, 'wt');     %% print it to an M-file
0180     if fd == -1
0181         error(['savecase: ', msg]);
0182     end
0183     
0184     %% function header, etc.
0185     if strcmp(mpc_ver, '1')
0186         if exist('areas', 'var') && exist('gencost', 'var') && ~isempty(gencost)
0187             fprintf(fd, 'function [baseMVA, bus, gen, branch, areas, gencost] = %s\n', rootname);
0188         else
0189             fprintf(fd, 'function [baseMVA, bus, gen, branch] = %s\n', rootname);
0190         end
0191         prefix = '';
0192     else
0193         fprintf(fd, 'function mpc = %s\n', rootname);
0194         prefix = 'mpc.';
0195     end
0196     if ~isempty(comment)
0197         if ischar(comment)
0198             fprintf(fd, '%%%s\n', comment);
0199         elseif iscell(comment)
0200             for k = 1:length(comment)
0201                 fprintf(fd, '%%%s\n', comment{k});
0202             end
0203         end
0204     end
0205     fprintf(fd, '\n%%%% MATPOWER Case Format : Version %s\n', mpc_ver);    
0206     if ~strcmp(mpc_ver, '1')
0207         fprintf(fd, 'mpc.version = ''%s'';\n', mpc_ver);    
0208     end
0209     fprintf(fd, '\n%%%%-----  Power Flow Data  -----%%%%\n');
0210     fprintf(fd, '%%%% system MVA base\n');
0211     fprintf(fd, '%sbaseMVA = %.9g;\n', prefix, baseMVA);
0212     
0213     %% bus data
0214     ncols = size(bus, 2);
0215     fprintf(fd, '\n%%%% bus data\n');
0216     fprintf(fd, '%%\tbus_i\ttype\tPd\tQd\tGs\tBs\tarea\tVm\tVa\tbaseKV\tzone\tVmax\tVmin');
0217     if ncols >= MU_VMIN             %% opf SOLVED, save with lambda's & mu's
0218         fprintf(fd, '\tlam_P\tlam_Q\tmu_Vmax\tmu_Vmin');
0219     end
0220     fprintf(fd, '\n%sbus = [\n', prefix);
0221     if ncols < MU_VMIN              %% opf NOT SOLVED, save without lambda's & mu's
0222         fprintf(fd, '\t%d\t%d\t%.9g\t%.9g\t%.9g\t%.9g\t%d\t%.9g\t%.9g\t%.9g\t%d\t%.9g\t%.9g;\n', bus(:, 1:VMIN).');
0223     else                            %% opf SOLVED, save with lambda's & mu's
0224         fprintf(fd, '\t%d\t%d\t%.9g\t%.9g\t%.9g\t%.9g\t%d\t%.9g\t%.9g\t%.9g\t%d\t%.9g\t%.9g\t%.4f\t%.4f\t%.4f\t%.4f;\n', bus(:, 1:MU_VMIN).');
0225     end
0226     fprintf(fd, '];\n');
0227     
0228     %% generator data
0229     ncols = size(gen, 2);
0230     fprintf(fd, '\n%%%% generator data\n');
0231     fprintf(fd, '%%\tbus\tPg\tQg\tQmax\tQmin\tVg\tmBase\tstatus\tPmax\tPmin');
0232     if ~strcmp(mpc_ver, '1')
0233         fprintf(fd, '\tPc1\tPc2\tQc1min\tQc1max\tQc2min\tQc2max\tramp_agc\tramp_10\tramp_30\tramp_q\tapf');
0234     end
0235     if ncols >= MU_QMIN             %% opf SOLVED, save with mu's
0236         fprintf(fd, '\tmu_Pmax\tmu_Pmin\tmu_Qmax\tmu_Qmin');
0237     end
0238     fprintf(fd, '\n%sgen = [\n', prefix);
0239     if ncols < MU_QMIN              %% opf NOT SOLVED, save without mu's
0240         if strcmp(mpc_ver, '1')
0241             fprintf(fd, '\t%d\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%d\t%.9g\t%.9g;\n', gen(:, 1:PMIN).');
0242         else
0243             fprintf(fd, '\t%d\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%d\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g;\n', gen(:, 1:APF).');
0244         end
0245     else
0246         if strcmp(mpc_ver, '1')
0247             fprintf(fd, '\t%d\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%d\t%.9g\t%.9g\t%.4f\t%.4f\t%.4f\t%.4f;\n', gen(:, 1:MU_QMIN).');
0248         else
0249             fprintf(fd, '\t%d\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%d\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%.4f\t%.4f\t%.4f\t%.4f;\n', gen(:, 1:MU_QMIN).');
0250         end
0251     end
0252     fprintf(fd, '];\n');
0253     
0254     %% branch data
0255     ncols = size(branch, 2);
0256     fprintf(fd, '\n%%%% branch data\n');
0257     fprintf(fd, '%%\tfbus\ttbus\tr\tx\tb\trateA\trateB\trateC\tratio\tangle\tstatus');
0258     if ~strcmp(mpc_ver, '1')
0259         fprintf(fd, '\tangmin\tangmax');
0260     end
0261     if ncols >= QT                  %% power flow SOLVED, save with line flows
0262         fprintf(fd, '\tPf\tQf\tPt\tQt');
0263     end
0264     if ncols >= MU_ST               %% opf SOLVED, save with mu's
0265         fprintf(fd, '\tmu_Sf\tmu_St');
0266         if ~strcmp(mpc_ver, '1')
0267             fprintf(fd, '\tmu_angmin\tmu_angmax');
0268         end
0269     end
0270     fprintf(fd, '\n%sbranch = [\n', prefix);
0271     if ncols < QT                   %% power flow NOT SOLVED, save without line flows or mu's
0272         if strcmp(mpc_ver, '1')
0273             fprintf(fd, '\t%d\t%d\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%d;\n', branch(:, 1:BR_STATUS).');
0274         else
0275             fprintf(fd, '\t%d\t%d\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%d\t%.9g\t%.9g;\n', branch(:, 1:ANGMAX).');
0276         end
0277     elseif ncols < MU_ST            %% power flow SOLVED, save with line flows but without mu's
0278         if strcmp(mpc_ver, '1')
0279             fprintf(fd, '\t%d\t%d\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%d\t%.4f\t%.4f\t%.4f\t%.4f;\n', branch(:, 1:QT).');
0280         else
0281             fprintf(fd, '\t%d\t%d\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%d\t%.9g\t%.9g\t%.4f\t%.4f\t%.4f\t%.4f;\n', branch(:, 1:QT).');
0282        end
0283     else                            %% opf SOLVED, save with lineflows & mu's
0284         if strcmp(mpc_ver, '1')
0285             fprintf(fd, '\t%d\t%d\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%d\t%.4f\t%.4f\t%.4f\t%.4f\t%.4f\t%.4f;\n', branch(:, 1:MU_ST).');
0286         else
0287             fprintf(fd, '\t%d\t%d\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%d\t%.9g\t%.9g\t%.4f\t%.4f\t%.4f\t%.4f\t%.4f\t%.4f\t%.4f\t%.4f;\n', branch(:, 1:MU_ANGMAX).');
0288         end
0289     end
0290     fprintf(fd, '];\n');
0291     
0292     %% OPF data
0293     if (exist('areas', 'var') && ~isempty(areas)) || ...
0294         (exist('gencost', 'var') && ~isempty(gencost))
0295         fprintf(fd, '\n%%%%-----  OPF Data  -----%%%%');
0296     end
0297     if exist('areas', 'var') && ~isempty(areas)
0298         %% area data
0299         fprintf(fd, '\n%%%% area data\n');
0300         fprintf(fd, '%%\tarea\trefbus\n');
0301         fprintf(fd, '%sareas = [\n', prefix);
0302         if ~isempty(areas)
0303             fprintf(fd, '\t%d\t%d;\n', areas(:, 1:PRICE_REF_BUS).');
0304         end
0305         fprintf(fd, '];\n');
0306     end
0307     if exist('gencost', 'var') && ~isempty(gencost)
0308         %% generator cost data
0309         fprintf(fd, '\n%%%% generator cost data\n');
0310         fprintf(fd, '%%\t1\tstartup\tshutdown\tn\tx1\ty1\t...\txn\tyn\n');
0311         fprintf(fd, '%%\t2\tstartup\tshutdown\tn\tc(n-1)\t...\tc0\n');
0312         fprintf(fd, '%sgencost = [\n', prefix);
0313         if ~isempty(gencost)
0314             n1 = 2 * max(gencost(gencost(:, MODEL) == PW_LINEAR,  NCOST));
0315             n2 =     max(gencost(gencost(:, MODEL) == POLYNOMIAL, NCOST));
0316             n = max([n1; n2]);
0317             if size(gencost, 2) < n + 4
0318                 error('savecase: gencost data claims it has more columns than it does');
0319             end
0320             template = '\t%d\t%.9g\t%.9g\t%d';
0321             for i = 1:n
0322                 template = [template, '\t%.9g'];
0323             end
0324             template = [template, ';\n'];
0325             fprintf(fd, template, gencost.');
0326         end
0327         fprintf(fd, '];\n');
0328     end
0329     
0330     %% generalized OPF user data
0331     if (isfield(mpc, 'A') && ~isempty(mpc.A)) || ...
0332             (isfield(mpc, 'N') && ~isempty(mpc.N))
0333         fprintf(fd, '\n%%%%-----  Generalized OPF User Data  -----%%%%');
0334     end
0335 
0336     %% user constraints
0337     if isfield(mpc, 'A') && ~isempty(mpc.A)
0338         %% A
0339         fprintf(fd, '\n%%%% user constraints\n');
0340         print_sparse(fd, sprintf('%sA', prefix), mpc.A);
0341         if isfield(mpc, 'l') && ~isempty(mpc.l) && ...
0342                 isfield(mpc, 'u') && ~isempty(mpc.u)
0343             fprintf(fd, 'lu = [\n');
0344             fprintf(fd, '\t%.9g\t%.9g;\n', [mpc.l mpc.u].');
0345             fprintf(fd, '];\n');
0346             fprintf(fd, '%sl = lu(:, 1);\n', prefix);
0347             fprintf(fd, '%su = lu(:, 2);\n\n', prefix);
0348         elseif isfield(mpc, 'l') && ~isempty(mpc.l)
0349             fprintf(fd, '%sl = [\n', prefix);
0350             fprintf(fd, '\t%.9g;\n', mpc.l);
0351             fprintf(fd, '];\n\n');
0352         elseif isfield(mpc, 'u') && ~isempty(mpc.u)
0353             fprintf(fd, '%su = [\n', prefix);
0354             fprintf(fd, '\t%.9g;\n', mpc.u);
0355             fprintf(fd, '];\n');
0356         end
0357     end
0358 
0359     %% user costs
0360     if isfield(mpc, 'N') && ~isempty(mpc.N)
0361         fprintf(fd, '\n%%%% user costs\n');
0362         print_sparse(fd, sprintf('%sN', prefix), mpc.N);
0363         if isfield(mpc, 'H') && ~isempty(mpc.H)
0364             print_sparse(fd, sprintf('%sH', prefix), mpc.H);
0365         end
0366         if isfield(mpc, 'fparm') && ~isempty(mpc.fparm)
0367             fprintf(fd, 'Cw_fparm = [\n');
0368             fprintf(fd, '\t%.9g\t%d\t%.9g\t%.9g\t%.9g;\n', [mpc.Cw mpc.fparm].');
0369             fprintf(fd, '];\n');
0370             fprintf(fd, '%sCw    = Cw_fparm(:, 1);\n', prefix);
0371             fprintf(fd, '%sfparm = Cw_fparm(:, 2:5);\n', prefix);
0372         else
0373             fprintf(fd, '%sCw = [\n', prefix);
0374             fprintf(fd, '\t%.9g;\n', mpc.Cw);
0375             fprintf(fd, '];\n');
0376         end
0377     end
0378 
0379     %% user vars
0380     if isfield(mpc, 'z0') || isfield(mpc, 'zl') || isfield(mpc, 'zu')
0381         fprintf(fd, '\n%%%% user vars\n');
0382     end
0383     if isfield(mpc, 'z0') && ~isempty(mpc.z0)
0384         fprintf(fd, '%sz0 = [\n', prefix);
0385         fprintf(fd, '\t%.9g;\n', mpc.z0);
0386         fprintf(fd, '];\n');
0387     end
0388     if isfield(mpc, 'zl') && ~isempty(mpc.zl)
0389         fprintf(fd, '%szl = [\n', prefix);
0390         fprintf(fd, '\t%.9g;\n', mpc.zl);
0391         fprintf(fd, '];\n');
0392     end
0393     if isfield(mpc, 'zu') && ~isempty(mpc.zu)
0394         fprintf(fd, '%szu = [\n', prefix);
0395         fprintf(fd, '\t%.9g;\n', mpc.zu);
0396         fprintf(fd, '];\n');
0397     end
0398 
0399     %% execute userfcn callbacks for 'savecase' stage
0400     if isfield(mpc, 'userfcn')
0401         run_userfcn(mpc.userfcn, 'savecase', mpc, fd, prefix);
0402     end
0403 
0404     %% close file
0405     if fd ~= 1
0406         fclose(fd);
0407     end
0408 end
0409 
0410 if nargout > 0
0411     fname_out = fname;
0412 end
0413 
0414 
0415 
0416 function print_sparse(fd, varname, A)
0417 
0418 [i, j, s] = find(A);
0419 [m, n] = size(A);
0420 
0421 if isempty(s)
0422     fprintf(fd, '%s = sparse(%d, %d);\n', varname, m, n);
0423 else
0424     fprintf(fd, 'ijs = [\n');
0425     if m == 1           %% i, j, s are row vectors
0426         fprintf(fd, '\t%d\t%d\t%.9g;\n', [i; j; s]);
0427     else                %% i, j, s are column vectors
0428         fprintf(fd, '\t%d\t%d\t%.9g;\n', [i j s].');
0429     end
0430     fprintf(fd, '];\n');
0431     fprintf(fd, '%s = sparse(ijs(:, 1), ijs(:, 2), ijs(:, 3), %d, %d);\n', varname, m, n);
0432 end

Generated on Mon 26-Jan-2015 15:00:13 by m2html © 2005