gusucode.com > symbolic工具箱matlab源码程序 > symbolic/convertMuPADNotebook.m
function matlabFile = convertMuPADNotebook(MuPADfile, MATLABLiveScript, varargin) %convertMuPADNotebook Convert MuPAD notebook file to MATLAB live script. % convertMuPADNotebook(MuPADfile, MATLABLiveScript) converts a MuPAD % notebook file MuPADfile (.mn) to a MATLAB live script MATLABLiveScript % (.mlx). Both MuPADfile and MATLABLiveScript must be full paths unless % the files are in the current folder. % % convertMuPADNotebook(MuPADfile) uses the same name and path, % MuPADfile, for the MATLAB live script file that contains converted % code. The extension .mn changes to .mlx in the resulting MATLAB live % script file. % % The function automatically adds the file extension .mn to MuPADfile % and .mlx to MATLABLiveScript if no corresponding file extension was % specified by the user. No other file extensions are accepted. % % The function returns the absolute file name of the MATLAB live script. % % The function displays a side-effect message % % Created 'TestDoc.mlx'. For verifying the document, see help. % % if the MuPAD notebook was converted successfully, or % % Created 'TestDoc.mlx'. 13 translation errors, 1 warnings. % For verifying the document, see help. % % if the MuPAD notebook was converted, but MuPAD code to MATLAB code % translation failed for some code regions. % In both cases the word 'help' provides a link to the documentation % that explains the final manual verification process. % % Examples: % >> lsf = convertMuPADNotebook('TestDoc.mn', 'results/TestDoc.mlx'); % Created 'TestDoc.mlx'. For verifying the document, see help. % >> edit(lsf); % % >> lsf = convertMuPADNotebook('TestDoc.mn'); % Created 'TestDoc.mlx'. For verifying the document, see help. % >> edit(lsf); % Copyright 2015-2016 The MathWorks, Inc. mFile = false; numOfargs = nargin; if numOfargs == 3 mFile = getOptions(varargin{1}); if mFile numOfargs = 1; end end if ~mFile narginchk(1,2); end % Is existing MuPAD Notebook with extension '.mn'? try validateattributes(MuPADfile, {'char'}, {'row'}); catch error(message('symbolic:convertMuPADNotebook:MustBeNotebookFileName')); end MuPADfile = sym.pathToFullPath(MuPADfile); [path,name,ext] = fileparts(MuPADfile); if isempty(ext) ext = '.mn'; MuPADfile = [MuPADfile ext]; end if ~strcmpi(ext, '.mn') || exist(MuPADfile, 'file') ~= 2 error(message('symbolic:convertMuPADNotebook:MustBeNotebookFileName')); end if isempty(name) || name(end) == ' ' error(message('symbolic:convertMuPADNotebook:InvalidNotebookFileName')); end if numOfargs == 2 % Check and complete MATLAB live script file name. try validateattributes(MATLABLiveScript, {'char'}, {'row'}); catch error(message('symbolic:convertMuPADNotebook:MustBeTargetFileName')); end MATLABLiveScript = sym.pathToFullPath(MATLABLiveScript); [opath,oname,oext] = fileparts(MATLABLiveScript); if isempty(oext) oext = '.mlx'; MATLABLiveScript = [MATLABLiveScript oext]; end if ~strcmp(oext, '.mlx') || exist(opath, 'dir') ~= 7 error(message('symbolic:convertMuPADNotebook:MustBeTargetFileName')); end if isempty(oname) || oname(end) == ' ' error(message('symbolic:convertMuPADNotebook:InvalidTargetFileName')); end matlabFile = MATLABLiveScript; else % For MuPAD notebook <path>.mn create a MATLAB live script <path>.mlx. matlabFile = [fullfile(path, name) '.mlx']; [~,oname,oext] = fileparts(matlabFile); end % Create a temporary folder for storing all temporary data. tempFolder = tempname; if ~mkdir(tempFolder) error(message('symbolic:convertMuPADNotebook:UnableToUnpackNotebook', MuPADfile)); end if ~mFile removeAllTempData = onCleanup(@() rmdir(tempFolder, 's')); end % Unpack MuPAD notebook in the temporary folder. notebookFolder = fullfile(tempFolder, name); unzip(MuPADfile, notebookFolder); if exist(notebookFolder, 'dir') ~= 7 error(message('symbolic:convertMuPADNotebook:UnableToUnpackNotebook', MuPADfile)); end % Read MuPAD notebook XML data and convert to MATLAB file ('.m'). % Create all temporary files in the temporary folder. notebookMuPAD = ['"' notebookFolder '.mn"']; notebookMuPAD = strrep(notebookMuPAD, '\', '/'); tempFolderMuPAD = ['"' tempFolder '"']; tempFolderMuPAD = strrep(tempFolderMuPAD, '\', '/'); try mupadConversionStatus = feval(symengine, 'export::mn2m', notebookMuPAD, sym('TargetFolder') == evalin(symengine, tempFolderMuPAD), 'CopyImages', 'NoWarnings'); catch error(message('symbolic:convertMuPADNotebook:UnableToConvertToScript', MuPADfile)); end matlabScript = [notebookFolder '.m']; if exist(matlabScript, 'file') ~= 2 error(message('symbolic:convertMuPADNotebook:UnableToConvertToScript', MuPADfile)); end if mFile matlabFile = matlabScript; return; else % Convert MATLAB file ('.m') to MATLAB live script ('.mlx'). try matlab.internal.richeditor.openAndSave(matlabScript, matlabFile); catch CaughtMException messageObj = message('symbolic:convertMuPADNotebook:UnableToConvertToLiveScript', strrep(MuPADfile, '\', '\\')); exceptionToThrow = MException('symbolic:convertMuPADNotebook:UnableToConvertToLiveScript', getString(messageObj)); exceptionToThrow.addCause(CaughtMException); throw(exceptionToThrow); end if exist(matlabFile, 'file') ~= 2 error(message('symbolic:convertMuPADNotebook:UnableToConvertToLiveScript', MuPADfile)); end % Display report fileLink = ['<a href="matlab:edit(''' matlabFile ''')">' oname oext '</a>']; if mupadConversionStatus(1) > 0 && mupadConversionStatus(2) > 0 report = message('symbolic:convertMuPADNotebook:ReportErrorsWarnings', ... fileLink, ... char(mupadConversionStatus(1)), ... char(mupadConversionStatus(2)) ... ); elseif mupadConversionStatus(1) > 0 report = message('symbolic:convertMuPADNotebook:ReportErrors', ... fileLink, ... char(mupadConversionStatus(1)) ... ); elseif mupadConversionStatus(2) > 0 report = message('symbolic:convertMuPADNotebook:ReportWarnings', ... fileLink, ... char(mupadConversionStatus(2)) ... ); else report = message('symbolic:convertMuPADNotebook:Report', ... fileLink ... ); end disp(getString(report)); end % parse options function opts = getOptions(name) opts = ischar(name) && strcmp(name,'-mFile'); end end