String to double conversion misunderstanding
34 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
fabrizio restori
il y a environ 15 heures
Commenté : fabrizio restori
il y a environ 11 heures
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 commentaires
Stephen23
il y a 17 minutes
Modifié(e) : Stephen23
il y a 16 minutes
"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.
Réponse acceptée
Cris LaPierre
il y a environ une heure
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 commentaires
Plus de réponses (1)
Steven Lord
il y a environ 11 heures
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 commentaires
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!