Peterstone
Peterstone

Reputation: 7449

MATLAB: Conversion from char data type to symbolic data type

Does anyone know how to make a conversion from char data type to symbolic data type? I put this:

x = 0.49;
n = 22;
roundn(exp(x*49/200),n)
class ans

and the answer is:

ans =

 0


ans =

char

I´m looking for the numeric value. How can I conver char data type to symbolic data type?

Thank you.

Upvotes: 0

Views: 3327

Answers (3)

John Alexiou
John Alexiou

Reputation: 29244

Is this what you are looking for?

%char
expr = '1+exp(1)';

%sym
x = sym(x);

%number
xval1 = double(x);

%rounded number
xval2 = double(vpa(x, 2));

>> n = 22;
>> expr = exp(x*49/200);
>> res = subs(expr,x,0.49);
res =
    1.1276
>> class(res)
res =
double
>> 

Upvotes: 1

gnovice
gnovice

Reputation: 125874

It sounds like you want the output from roundn(...) to be a symbolic expression. However, ROUNDN appears to be a function from the Mapping Toolbox, and thus I doubt it will work with symbolic variables.

I think maybe using VPA from the Symbolic Toolbox is what you want:

>> eq = sym('exp(x*49/200)');  %# A symbolic equation
>> x = sym(0.49);              %# A symbolic value
>> n = 22;                     %# Number of digits of precision
>> vpa(subs(eq,'x',x),n)       %# Substitute x and evaluate

ans =

1.127553227831349194548        %# ans is a symbolic value

Upvotes: 1

eykanal
eykanal

Reputation: 27027

The output is of the correct class; your problem is in your syntax. Use:

class(ans)

instead of

class ans

The first gives you the class of the variable ans, the second gives you the class of the string "ans".

Upvotes: 2

Related Questions