www.gusucode.com > mbc 工具箱 matlab 源码程序 > mbc/+mbcutils/splitEquation.m

    function strSplit = splitEquation(strIn, numChars)
%splitEquation Split string into 80 character chunks
%
%  strSplit=splitEquation(strIn, numChars) creates a multi-line output string c 
%   where no line of th string is more than numChars chars and the string is
%   prefferably split by + or - then by * character.

%  Copyright 2014 The MathWorks, Inc. and Ford Global Technologies, Inc.

% divide string into lines of numChars characters
strSplit={};
while length(strIn)>numChars
    strChunk = fliplr(strIn(1:numChars));
    % line should end with '+' or '-'
    splitPositions = sort([strfind(strChunk,'+') strfind(strChunk,'-')]);
    if ~isempty(splitPositions)
        [strIn,strSplit] = splitString(strChunk,splitPositions,strIn,strSplit);
    else
        % otherwise should end on a '*'
        splitPositions = sort(strfind(strChunk,'*'));
        if ~isempty(splitPositions)
            [strIn,strSplit] = splitString(strChunk,splitPositions,strIn,strSplit);
        else
            % otherwise split the string after the first 80 chars
            [strIn,strSplit] = splitString(strChunk,1,strIn,strSplit);
        end
    end
end
if strIn(1)==' ';
    strIn = strIn(2:end);
end
strSplit = [strSplit ; {strIn}];


function [strIn,strSplit] = splitString(strChunk,splitPositions,strIn,strSplit)
strChunk = fliplr(strChunk(splitPositions(1):end));
strIn = strIn(length(strChunk)+1:end);
if strChunk(1)==' ';
    strChunk= strChunk(2:end);
end
strSplit = [strSplit ; {strChunk}];