Azzera filtri
Azzera filtri

How to convert String to Double ?

12 visualizzazioni (ultimi 30 giorni)
Rushil Shah
Rushil Shah il 8 Giu 2020
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 Commenti
Adam Danz
Adam Danz il 8 Giu 2020
Are the strings variables names? In other words, are you trying to convert "ux" into the number stored by variable ux?
Rushil Shah
Rushil Shah il 8 Giu 2020
Yes I am using them as variables.
Here's the entire code.
list=["ux","uy","t","u","theta","x","y","r","R","H","th"]
% indx is index of the variables chosen by the user
% answer is the array which contains value of the variables chosen by the user
var=list(indx);
val=answer;
for n=1:numel(var) % this is the code to assign value from answer to the variables created from var string array
vn=val(n);
assignin('base',char(var(n)),vn{:})
end
class(var)
Here every variable has 'char' datatype which can't be used for calculations.
That's why I need them to convert it into double format.

Accedi per commentare.

Risposte (1)

Walter Roberson
Walter Roberson il 8 Giu 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 Commenti
Rushil Shah
Rushil Shah il 9 Giu 2020
Thanks for your positive respose.
I tried your code but it is showing error and not giving any solutions back. I love the idea of using 'menu' function. 'vpasolve' is working better and faster than this method. We need to solve 11 equations so subsituting the vaues isn't working.
CODE
syms ux uy t u theta x y r R H th
g=-9.81;
eqns=[theta==acosd(ux/u),ux == u*cosd(theta),uy == u*sind(theta)-g*t,u==sqrt((ux.^2+uy.^2)),t==(2*u*sind(theta))/g,x==u*t*cosd(theta),y==u*t*sind(theta)-(g*t.^2)/2,r==sqrt((x.^2+y.^2)),th==(u*sind(theta))/g,H==(u.^2*sind(theta)^2)/(2*g),R==(u.^2*sind(2*theta))/g]
list = ["ux","uy","t","u","theta","x","y","r","R","H","th"] % variables
var1idx = menu('Pick a variable', list);
var1 = list(var1idx);
var1val = input("enter a value for " + var1 + " ? ");
list2 = list;
list2(var1idx) = [];
var2idx = menu('Pick a second variable', list2);
var2 = list2(var2idx);
var2val = input("enter a value for " + var2 + " ? ");
symlist = sym(list);
vallist = subs(symlist, {var1, var2}, {var1val, var2val});
sol=solve(eqns,[ux uy t th r R H u theta x y])
% equations=[H==(u.^2*sind(theta)^2)/(2*g),R==(u.^2*sind(2*theta))/g,t==(2*u*sind(theta))/g,th==(u*sind(theta))/g]
% AB = vpasolve(eqns1,[u theta t th])
new_equations = subs(sol, symlist, vallist);
new_equations
ERROR SHOWN
Warning:
Unable to solve symbolically.
Returning a numeric solution using vpasolve.
Error using subs
Expected input number 1, S, to be one of these types:
sym
Instead its type was struct.
Error in sym/subs (line 60)
validateattributes(F, {'sym'}, {}, 'subs', 'S', 1);
Please help to change the datatype of those variables.
Walter Roberson
Walter Roberson il 9 Giu 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.

Accedi per commentare.

Prodotti


Release

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by