Warning: Error updating Text

2k Views Asked by At

MATLAB gives me a warning as following:

Warning: Error updating Text.

Character vector must have valid interpreter syntax:
$\Theta(s) = 9.24\cdot 10^{04 }\cdot \frac{s +    0}{s^{4} +  140s^{3} + 2.35\cdot 10^{03s}^{2} + 9.24\cdot 10^{04s} +    0}$

I'm not sure why it is invalid syntax. I'm using the following code:

numericTF = struct;
symbolTF = struct;

if strcmp(vars.response_type,'Position')
    typeString = 'Theta(s)';
else
    typeString = 'Omega(s)';
end

tf4thOnum = [1 I/P];
tf4thOden = [1 (b*L+R*J)/J/L (R*b+ Kb*Km)/J/L 2*Km*P/J/L I*2*Km/J/L];
tf4thOgain = 2*Km*P/J/L;
tf4thOstr = sprintf('$\\%s = %4.3g \\cdot \\frac{s + %4.3g}{s^{4} + %4.3gs^{3} + %4.3gs^{2} + %4.3gs + %4.3g}$', typeString, tf4thOgain,tf4thOnum(2:end),tf4thOden(2:end));
tf4thOsym = '\frac{k_ak_mk_p}{JL}\cdot\frac{s+\frac{k_i}{k_p}}{s^4+\frac{bL+RJ}{JL}s^3+\frac{bR+k_bk_m}{JL}s^2+\frac{k_ak_mk_p}{JL}s+\frac{k_ik_mk_ap}{JL}}$';

Another function to add scientific notation:

function instring = cleanExps(instring)
        exps = findstr(instring,'e+');
        maxn = length(exps);
        if ~isempty(exps)
            for ii = 1:maxn
                if isempty(exps) break; end
                instring = strrep(instring,instring(exps(1):exps(1)+4),['\cdot 10^{' instring(exps(1)+2:exps(1)+4) '}']);
                exps = findstr(instring,'e+');
            end
        end
        exps = findstr(instring,'e-');
        maxn = length(exps);
        if ~isempty(exps)
            for ii = 1:maxn
                if isempty(exps) break; end
                instring = strrep(instring,instring(exps(1):exps(1)+4),['\cdot 10^{-' instring(exps(1)+2:exps(1)+4) '}']);
                exps = findstr(instring,'e-');
            end
        end
1

There are 1 best solutions below

4
On BEST ANSWER

I assume from the warning message that you are trying to set the string of a text object to the following (which I've broken up into multiple lines for clarity):

str = ['$\Theta(s) = 9.24\cdot 10^{04}\cdot \frac{s + 0}{s^{4} + 140s^{3}' ...
       ' + 2.35\cdot 10^{03s}^{2} + 9.24\cdot 10^{04s} + 0}$'];

There's at least one of two things going wrong here...

First, you need to make sure that the 'Interpreter' property of the text object is set to 'latex' instead of the default 'tex', since you are using LaTeX markup.

Second, there are two errors in the syntax of your terms, noted here:

'... + 2.35\cdot 10^{03s}^{2} + 9.24\cdot 10^{04s} + 0}$'
                        ^                        ^

Those two curly braces should be on the other side of the s. The first one causes your syntax error since 10^{...}^{...} doesn't make sense. The second one just wrongly puts s in the exponent.

Those two errors appear to be the result of a bug in your cleanExps function. Every +4 appearing in that function should be a +3 to keep it from including one too many characters after the exponent. If you're interested, a more robust and potentially faster alternative to your function would be this one call to regexprep:

instring = regexprep(instring, 'e([+|-]+)(\d+)', '\\cdot 10^{${setdiff($1,''+'')}$2}');

After fixing the bug and ensuring you are using the latex interpreter setting, everything should work fine. For example:

str = ['$\Theta(s) = 9.24\cdot 10^{04}\cdot \frac{s + 0}{s^{4} + 140s^{3}' ...
       ' + 2.35\cdot 10^{03}s^{2} + 9.24\cdot 10^{04}s + 0}$'];
hText = text(0.1, 0.5, str, 'Interpreter', 'latex', 'FontSize', 14);

enter image description here