How to convert a char letter into a double which is still a letter.

4 Ansichten (letzte 30 Tage)
How do i convert this variable:
graph which is a char with value = t
into a variable that remains with the same value = t, but it is read by Matlab as a double.
I´ve been trying to use str2double but it returns a NAN. If I use str2num it returns an empty vector.
I need to use the t value as a double in order to graph with the area() function.
Help please!!!
  1 Kommentar
Alfonso Rodriguez
Alfonso Rodriguez am 13 Jul. 2020
Thanks Walter, Image analyst and Stephen. I am trying to make a GUI that graphs the area under the curve of a syms f(x) (user) function. The area function only works for numerical integration so I can´t use it with a symbolic integration.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 10 Jul. 2020
If you are getting user inputs that are formula, then you should use str2func() to turn them into function handles.
Alternately, if you have the symbolic toolbox, you can use str2sym() to turn them into symbolic expressions.
  4 Kommentare
Walter Roberson
Walter Roberson am 14 Jul. 2020
S = get(handles.edit1, 'string');
v = symvar(S);
func = str2func( sprintf('@(%s) %s', v{1}, S) );
Alfonso Rodriguez
Alfonso Rodriguez am 15 Jul. 2020
Thanks Again Walter. I didn´t knew symvar() existed.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Image Analyst
Image Analyst am 10 Jul. 2020
What value do you want t to have when being considered as a number? It's ASCII value? It's position in the alphabet (20)? Perhaps this helps:
ch = 't'
n = ch - 'a' + 1
ch =
't'
n =
20
Or do you simply want a variable named "t" that has a value you want, like 10, such that you could do this:
t = 10 % t now has the value 10
t2 = t * 2 % Multiply t by 2 to get 20
t =
10
t2 =
20
I have no idea what you mean when you say " t value as a double in order to graph with the area() function". Is t an N-by-2 list of (x,y) coordinates?
Please explain what you mean in more words, and with a desired output diagram/graph, but only after you read this link.
  2 Kommentare
Alfonso Rodriguez
Alfonso Rodriguez am 13 Jul. 2020
Thanks Image analyst. I am trying to make a GUI that graphs the area under the curve of a syms f(x) (user) function. The area function only works for numerical integration so I can´t use it with a symbolic integration.
I wanted to get the user "string" func = get(edit, string) and then convert it to a numerical function that has x and y values in order to graph the area under the curve with the area() function.
Image Analyst
Image Analyst am 14 Jul. 2020
Seems like you should have accepted Walter's answer rather than mine.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Symbolic Math Toolbox finden Sie in Help Center und File Exchange

Tags

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by