xxsimOpenModel - Opens a 20-sim model. sets the opened 20-sim model as active model and assigns it unique identifier. Syntax: identifier = xxsimOpenModel(fileName, boolean) Inputs: fileName = directory path of the model to be opened . Note: entering an empty string opens an empty 20-sim editor. boolean = (optional) indicates if a new 20-sim editor should be opened. true - opens model with a new editor. false - opens model with an existing editor (Default). Outputs: identifier = opened 20-sim editors are assigned identifier value (1-based). in case of error, 0 is returned. Examples: identifier = xxsimOpenModel('C:\temp\test.emx',true) - opens model 'test.emx' with a new 20-sim editor. See also: xxsimCloseModel Author: Controllab Products B.V. email: info@controllab.nl Website: http://www.controllab.nl September 2013
0001 function [identifier] = xxsimOpenModel(varargin) 0002 % xxsimOpenModel - Opens a 20-sim model. 0003 % sets the opened 20-sim model as active model and assigns it unique identifier. 0004 % 0005 % Syntax: 0006 % identifier = xxsimOpenModel(fileName, boolean) 0007 % 0008 % Inputs: 0009 % fileName = directory path of the model to be opened . 0010 % Note: entering an empty string opens an empty 20-sim editor. 0011 % 0012 % boolean = (optional) indicates if a new 20-sim editor should be opened. 0013 % true - opens model with a new editor. 0014 % false - opens model with an existing editor (Default). 0015 % 0016 % Outputs: 0017 % identifier = opened 20-sim editors are assigned identifier value (1-based). 0018 % in case of error, 0 is returned. 0019 % 0020 % Examples: 0021 % identifier = xxsimOpenModel('C:\temp\test.emx',true) 0022 % 0023 % - opens model 'test.emx' with a new 20-sim editor. 0024 % 0025 % See also: xxsimCloseModel 0026 % 0027 % Author: Controllab Products B.V. 0028 % email: info@controllab.nl 0029 % Website: http://www.controllab.nl 0030 % September 2013 0031 0032 %------------- BEGIN CODE -------------- 0033 %check input arguments 0034 if length(varargin)==0 0035 error('Not enough input arguments.'); 0036 elseif length(varargin)>2 0037 error('Too many input arguments.'); 0038 end 0039 0040 if ~ischar(varargin{1}) 0041 error('Input argument 1 should be a file name.'); 0042 else 0043 modelFilename = varargin{1}; 0044 end 0045 0046 if length(varargin) == 2 0047 if ~islogical(varargin{2}) 0048 error('Input argument 2 should be a boolean.'); 0049 end 0050 0051 newinstance = varargin{2}; 0052 else 0053 newinstance = false; 0054 end 0055 0056 % first set on 0 indicating wrong identifier 0057 identifier = 0; 0058 0059 % check if we already have a connection 0060 global xxsim; 0061 if (~exist('xrlisconnected')) || xrlisconnected() == 0 || length(xxsim) == 0 || length(xxsim.connections) == 0 0062 xxsimConnect(); 0063 0064 % now a connection must have been made 0065 if xrlisconnected() == 0 0066 error('no connection to 20-sim could be made.'); 0067 end 0068 end 0069 0070 %check if the model file exists 0071 0072 if length(modelFilename) > 0 0073 [status, result, msgid] = fileattrib(modelFilename); 0074 if status == 1 0075 % use the full resolved name, and we're done 0076 useFilename = fullfile(result.Name, ''); 0077 else 0078 % check if the user provided a full name 0079 [directory, name, extension] = fileparts(modelFilename); 0080 0081 % is an extension given? 0082 if length(extension) == 0 0083 extension = '.emx'; 0084 end 0085 0086 % is a directory given? 0087 if length(directory) == 0 0088 % use the current directory 0089 directory = pwd; 0090 end 0091 0092 useFilename = fullfile(directory, [name, extension]); 0093 end 0094 0095 0096 % again check if we have a valid name now 0097 if exist(useFilename, 'file') == 0 0098 error('%s does not exist.', modelFilename); 0099 end 0100 0101 if xxsim.verbose 0102 0103 % do some verbose output 0104 fprintf('Opening file: %s\n', useFilename); 0105 0106 end 0107 else 0108 useFilename = ''; 0109 end 0110 0111 try 0112 reply = xrlinvoke('xxsim.openModel', struct('name', useFilename, 'newinstance', newinstance )); 0113 if (length(reply) == 0 ) 0114 error('Could not load the model'); 0115 end 0116 if length(useFilename) > 0 0117 if xxsim.verbose 0118 0119 % do some verbose output 0120 fprintf('Successfully opened file: %s\n', useFilename); 0121 0122 end 0123 end 0124 0125 % return the identifier 0126 identifier = reply.identifier; 0127 catch 0128 error('Could not load the model'); 0129 end 0130 0131 end 0132 %------------- END OF CODE --------------