transform to input to symbol

Dear all
how can i creat a function which take any desired variable like x or y or z and transform it to a symbol. i mean the function take the x and give back to symbol x.
thanks alot

Answers (1)

syms(sym('x')) % works only for scalar variables , bearing in mind this is only used for symbolic calculations.

6 Comments

saeed rezaei
saeed rezaei on 2 Apr 2019
Edited: saeed rezaei on 2 Apr 2019
thanks for your helping
let me explain my problem exactly with another example.
suppose that we created a function ( saeed function) which takes an expression and a number. then calculates the expression based on the number and gives back the result. for example:
>>saeed(x^2+x-5,2)
ans:
1
the function is like this
function [out]=saeed(eq,n)
the problem is that when user enter this : saeed(x^2+x-5,2) the matlab gives an error. the error is:
Undefined function or variable 'x'.
as you know we can define x as a symbol. but we can do this befor function. now, what should i do ?
saeed?? Upload the function.
saeed is a desirable name. its my name :)
function [out]=saeed(eq,n)
clc
clear all
eqn=eq(n)
end
eq=input('Enter the equation:','s') % Type x^2+x-5
n=2;
eqn = str2func(['@(x)',eq]);
eqn(n)
Note: Remove clc and clear all inside your function.
yeah
i know this way. but this code is not a function.
i wanna write the code as a function so that the user can enter saeed(eq,n) and take back the answer
eq=input('Enter the equation:','s') % Type x^2+x-5
n=2;
Result = saeed(eq,n)
function Result=saeed(eq,n)
eqn = str2func(['@(x)',eq]);
Result=eqn(n);
end

Sign in to comment.

Categories

Products

Release

R2015a

Asked:

on 2 Apr 2019

Commented:

on 2 Apr 2019

Community Treasure Hunt

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

Start Hunting!