String to double conversion misunderstanding
34 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
fabrizio restori
hace alrededor de 17 horas
Comentada: fabrizio restori
hace alrededor de 13 horas
I'm modifying on table imported from csv, the fields (rep.ACVoltageR), of course, are char. I try to modify the fields to double, using
tensione = str2double(rep.ACVoltageR);
but tensione is still as a char and not a double.
Were is may mistake?
>> class ACVoltageR
ans =
'char'
>> class tensione
ans =
'char'
Thanks, Fabrizio
3 comentarios
Stephen23
hace 44 minutos
Editada: Stephen23
hace 43 minutos
"I'm modifying on table imported from csv, the fields (rep.ACVoltageR), of course, are char."
Why "of course" ?
Most likely it would be much simpler and/or robust to import that numeric data correctly as numeric data. This is the approach that I strongly recommend. If you upload your data by clicking the paperclip button then we can help you with that.
"Were is may mistake?"
Possibly you tried allocating the numeric data to a table variable/column which has class character. But as you have not shown us your actual code, we will just have to guess.
Respuesta aceptada
Cris LaPierre
hace 43 minutos
You are checking the class of a character array, not your variable. You need to change your syntax to check the class of your variables.
% Create a char that represents a numberic value
rep.ACVoltageR = '4.8'
% Convert to a double
tensione = str2double(rep.ACVoltageR)
% Check the class of char arrays ## incorrect syntax ##
class ACVoltageR
class tensione
% Check the class of values stored in your variables
class(rep.ACVoltageR)
class(tensione)
0 comentarios
Más respuestas (1)
Steven Lord
hace alrededor de 11 horas
The mistake is that you're using command form for calling the class function, not function form. These:
class ACVoltageR
class tensione
are equivalent to:
class('ACVoltageR')
class('tensione')
Also note that at this point in the execution my answer, there are no variables named ACVoltageR and tensione in the workspace!
whos ACVoltageR tensione ans % only ans is listed
If you want to ask for the class of the variable named tensione, use function form passing in the variable not its name.
tensione = str2double('42');
class(tensione)
whos ACVoltageR tensione ans % ans and tensione are listed, ACVoltageR still doesn't exist
0 comentarios
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!