String function derivative

2 views (last 30 days)
InterNeo
InterNeo on 2 Apr 2012
I'm trying to do
F_diff_x = matlabFunction(diff(sym(f), 'x'));
when f is
f = get(handles.equation,'String');
And sometimes I can calculate the derivative, sometimes not Becouse I get:
??? Error using ==> sym.sym>convertExpression at 2515
Error: 'expression' expected [line 1, col 12]
Error in ==> sym.sym>convertChar at 2426
s = convertExpression(x);
Error in ==> sym.sym>convertCharWithOption at 2410
s = convertChar(x);
Error in ==> sym.sym>tomupad at 2164
S = convertCharWithOption(x,a);
Error in ==> sym.sym>sym.sym at 111
S.s = tomupad(x,'');
Error in ==> moo>bc_Callback at 383
F_diff_x = matlabFunction(diff(sym(f), 'x'));
How can I do this in a different way?
  2 Comments
Walter Roberson
Walter Roberson on 2 Apr 2012
Could you give us an example of a string that you get the error for?
InterNeo
InterNeo on 3 Apr 2012
'sin(x)+cos(2*y)' - works
'sin(x)+x.^2+cos(2*y)' - doesn't work

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 3 Apr 2012
The dot operators are not valid MuPAD code, and it is MuPAD code that must be sym()'d, not MATLAB code.
Not 'sin(x)+x.^2+cos(2*y)' but 'sin(x)+x^2+cos(2*y)'
  3 Comments
Walter Roberson
Walter Roberson on 3 Apr 2012
There is no good reason to mix inline and matlabFunction. Just use
iF = matlabfunction(sym(f),'X','Y');
If you feel you must use inline, then use a vectorize() call:
iF = inline(vectorize(f));
InterNeo
InterNeo on 3 Apr 2012
It works great. Thank you very much!

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 2 Apr 2012
Before using f, check ischar(f) . I suspect you will find that in the cases you are getting errors for, that f will be a cell array of strings rather than a string itself.
The String property of a uicontrol style 'edit' will be a string or char array if the property was initialized with a string or with a char array, but the property will be a cell array of strings if the property was initialized with a cell array.
  1 Comment
InterNeo
InterNeo on 3 Apr 2012
ischar(f) is true in both the above examples

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!