How to convert String to Double ?
12 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I am trying to convert a string array into the double array.
var = 1×11 string
"ux" "uy" "t" "u" "theta" "x" "y" "r" "R" "H" "th"
This is the string array i want to convert in double array
Required output:
var = 1×11 double
ux uy t u theta x y r R H th
I tried the following code :
vard=str2double(var)
var=vard
And this is its output
var = 1×10
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
Thanks for your time and consideration.
Thanks in advance for helping me to solve this issue.
2 comentarios
Adam Danz
el 8 de Jun. de 2020
Are the strings variables names? In other words, are you trying to convert "ux" into the number stored by variable ux?
Respuestas (1)
Walter Roberson
el 8 de Jun. de 2020
You cannot. Names such as ux can never be data class double. The closest you could get would be categorical(var)
If what you mean is that you have a string array of names, and for each name there is variable with that name in the workspace, and you wish to replace each name with the content of the variable with that name, then if you have the Symbolic Toolbox, use subs()
5 comentarios
Walter Roberson
el 9 de Jun. de 2020
Returning a numeric solution using vpasolve.
You solve()'d all 11 original equations for all 11 variables. MATLAB was not able to find all the exact solutions, or even one exact solution, so it returned one numeric solution. That numeric solution will not have any variables in it. Numeric solutions returned by solve() or vpasolve() never have remaining variables in them.
The only time you can get a solution with remaining variables is if you solve() for fewer variables than you have equations and solve() is able to find an analytic form for the variables you solve for in terms of the remaining variables.
new_equations = subs(sol, symlist, vallist);
sol is your struct that has a single numeric solution. It has no variables in it to have their variables substituted. But if you insist on trying, then
new_sol = structfun(@(S) subs(S, symlist, vallist), sol, 'uniform', 0);
You will see that the result is exactly the same set of numeric values as the original struct.
We need to solve 11 equations so subsituting the vaues isn't working.
You have 11 equations in 11 variables. If there are solutions at all, then it will only be when the values of the variables have very precise relationships to each other. But you are having the user enter 2 arbitrary numeric values for variables, using whatever values that they want, and those arbitrary numeric values that they enter will almost certainly not have the necessary precise relationships to each other. There will be no solution nearly all of the time.
If you want to be able to have the user provide exact values for two of the variables, then you need to remove two of the equations, each involving at least one of the two variables.
Because you have trig in your equations, there is something meaningful you can do with user-provided values: you can use the user-provided values as starting points for vpasolve. Not as exact values, but as starting points to help find a close-by period of the values. For example,
symlist = sym(list);
vallist = subs(symlist, {var1, var2}, {var1val, var2val});
vallist(ismember(vallist, symvar(vallist))) = nan;
sol = vpasolve(eqns, symlist, vallist(:));
Due to the way that vpasolve() finds values, the solution that it finds will not necessarily be the one "closest" to the initial values that the user provides.
And to emphasize, the solutions found will almost certainly not have the corresponding variable have exactly the value provided by the user.
Consider for example vpasolve(x^2+sin(x)==4, x) then there are multiple solutions, and if you have the user provide an initial value, vpasolve(x^2==4, x, INITIALVALUE) then if the INITIALVALUE is positive then typically a solution near +2 would be found, and if the INITIALVALUE is negative then typically a solution near -2 would be found: the user is not providing the exact solution, just a clue to vpasolve() of where to look, and vpasolve() might or might not be able to take advantage of that clue.
Ver también
Categorías
Más información sobre Symbolic Variables, Expressions, Functions, and Preferences en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!