a='name'
syms (a)
this code generates a symbol called 'name'. If I write a function, I don't know the string before I use it, so how can I use the symbol 'name' (like symbol) into the function?
i.e
function [A]=f(a)
a='name';
syms (a)
c=cos(name);
s=sin(name);
A=c+s;
In the output I want a symbolic expression A=cos(name) + sin(name).
Thank you a lot for you answer. Pietro Rossi

 Akzeptierte Antwort

Walter Roberson
Walter Roberson am 11 Mai 2011

0 Stimmen

I would try
function [A]=f(a)
%a='name'; %I presume this will be passed in
syms (a)
c=cos(a);
s=sin(a);
A=c+s;

3 Kommentare

Pietro Rossi
Pietro Rossi am 11 Mai 2011
The function is:
function [A]=f(a)
syms (a)
c=cos(a);
s=sin(a);
A=c+s;
So, in the workspace I wrote:
a = 'nome';
A=f(a)
but Matlab sayd:
??? Undefined function or method 'cos' for input arguments of type 'char'.
Error in ==> fprova at 4
c=cos(a);
I tried in assign "syms name" before using f function:
syms nome
a = 'nome';
A=f(a)
:(
Walter Roberson
Walter Roberson am 11 Mai 2011
Ah, in that case,
function [A]=f(a)
as = syms (a)
c=cos(as);
s=sin(as);
A=c+s;
Pietro Rossi
Pietro Rossi am 11 Mai 2011
Thanks so mush! The function run now! :)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by