Struggling to retrieve table data as numeric values

1 view (last 30 days)
Ernst
Ernst on 4 Jul 2022
Commented: Walter Roberson on 5 Jul 2022
I have this table (newData) with column headers as ex. xVal, dtg, rpm etc. The values looks like:
xVal dtg rpm etc.
1 '1654668291510' '1047.47' ...
2 '1654668291610' '1047.59' ...
...
etc
I'm struggling to convert the column data to numeric values. Ex. the rpm data should be put in an array of double. I've tried str2num and str2double but I keep getting NaN for some reason that I can't figure out...
Any ideas?
  6 Comments
Walter Roberson
Walter Roberson on 5 Jul 2022
Your csv file does not have column headers such as xVal, dtg, rpm etc . Instead, it has lines such as
GVL_LogData.aLogData2[10, 0];'1654668292510';
in which the word GVL_LogData.aLogData2 is constant, and the part in the [] appears to be a 2D subscript (0 based), and the part in quotes appears to be the associated value. The data appears to be 6000 x 9.
Is it possible that those 9 columns correspond to xVal, dtg, and so on? If so then we need a list of what the column order is.

Sign in to comment.

Accepted Answer

Karim
Karim on 4 Jul 2022
Hello, normally str2double should work. See below for an example.
% allocate the table
varTypes = ["double","string","string"]; % data type for each column
varNames = ["xVal","dtg","rpm"]; % variable name for each column
MyTable = table('Size',[2 3],'VariableTypes',varTypes,'VariableNames',varNames);
% store data in the table
MyTable(1,:) = {1 , '1654668291510' , '1047.47'};
MyTable(2,:) = {2 , '1654668291610' , '1047.59'};
% have a look at the content of the table
MyTable
MyTable = 2×3 table
xVal dtg rpm ____ _______________ _________ 1 "1654668291510" "1047.47" 2 "1654668291610" "1047.59"
% convert the columns to double
MyTable.dtg = str2double(MyTable.dtg);
MyTable.rpm = str2double(MyTable.rpm);
% have a look at the table
MyTable
MyTable = 2×3 table
xVal dtg rpm ____ __________ ______ 1 1.6547e+12 1047.5 2 1.6547e+12 1047.6
% extract the rpm colum to a new array
RpmArray = MyTable.rpm
RpmArray = 2×1
1.0e+03 * 1.0475 1.0476
  3 Comments
Ernst
Ernst on 5 Jul 2022
Hi Karim (and Stephen23)
Thank you for your replies. Stephen23 made me curious about the import, and Karim gave me the syntax on the convertion of data. Also you gave me some good information regarding the work with table data.
I'm suspecting that I also have a problem related to the fact that my windows is actually a danish version, and I think there is also some kind of issue with the type of comma (. vs ,). In some cases I get the correct value, and in some cases I get a faulty value.
So for now - thank you very much for your replies. It helped me to move on with the data handling - thanks.

Sign in to comment.

More Answers (0)

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!