String to double conversion misunderstanding

31 views (last 30 days)
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 Comments
Stephen23
Stephen23 1 minute ago
Edited: Stephen23 1 minute ago
"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.
fabrizio restori
fabrizio restori 4 minutes ago
I found my mistake, thanks to yours comments, I've better understand the import procedure (I'm new to Matlab). Now I don't need any conversion, I've just redefined data type during import.
My main error was the missuses of class function:
class ACVoltage
instead of
class (ACVoltage)
A very beginner error, worse than normally. :-(

Sign in to comment.

Accepted Answer

Cris LaPierre
Cris LaPierre 32 minutes ago
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'
rep = struct with fields:
ACVoltageR: '4.8'
% Convert to a double
tensione = str2double(rep.ACVoltageR)
tensione = 4.8000
% Check the class of char arrays ## incorrect syntax ##
class ACVoltageR
ans = 'char'
class tensione
ans = 'char'
% Check the class of values stored in your variables
class(rep.ACVoltageR)
ans = 'char'
class(tensione)
ans = 'double'

More Answers (1)

Steven Lord
Steven Lord 30 minutes ago
The mistake is that you're using command form for calling the class function, not function form. These:
class ACVoltageR
ans = 'char'
class tensione
ans = 'char'
are equivalent to:
class('ACVoltageR')
ans = 'char'
class('tensione')
ans = 'char'
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
Name Size Bytes Class Attributes ans 1x4 8 char
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)
ans = 'double'
whos ACVoltageR tensione ans % ans and tensione are listed, ACVoltageR still doesn't exist
Name Size Bytes Class Attributes ans 1x6 12 char tensione 1x1 8 double

Community Treasure Hunt

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

Start Hunting!