Struggling to retrieve table data as numeric values
이전 댓글 표시
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
"I have this table (newData) ... I'm struggling to convert the column data to numeric values."
How did you create that table in the first place? If you imported it from file, you could probably import the numeric data as numeric. Please upload a sample data file by clicking the paperclip button.
Ernst
2022년 7월 5일
thanks for the file share.
load('data.mat')
data
data.VarName1(3)
data.INPUT(3)
As you can see you imported the data as categorical, this is why you cannot use str2double. To convert the column "INPUT" to doubles, you will need to delete the first two elements, afterwards you can use the double command:
data([1 2],:) = [];
data.INPUT = double( data.INPUT )
However, is this the intend? Can you share the original data, right now i have the impression that something not correct with the import.
Ernst
2022년 7월 5일
In that case you can indeed import the "OnlineValue" data as double. See below for an import methodology, note that this was generated in 2020a. You can generate a similair script by using the "import data" button in the home tab.
%% Setup the Import Options and import the data
opts = delimitedTextImportOptions("NumVariables", 2);
% Specify range and delimiter
opts.DataLines = [8, Inf];
opts.Delimiter = ";";
% Specify column names and types
opts.VariableNames = ["Variable", "OnlineValue"];
opts.VariableTypes = ["string", "double"];
% Specify file level properties
opts.ExtraColumnsRule = "ignore";
opts.EmptyLineRule = "read";
opts.ConsecutiveDelimitersRule = "join";
% Specify variable properties
opts = setvaropts(opts, "Variable", "WhitespaceRule", "preserve");
opts = setvaropts(opts, "Variable", "EmptyFieldRule", "auto");
opts = setvaropts(opts, "OnlineValue", "TrimNonNumeric", true);
opts = setvaropts(opts, "OnlineValue", "ThousandsSeparator", ",");
% Import the data
MyData = readtable("Stop 080622 0600.csv", opts);
MyData
Walter Roberson
2022년 7월 5일
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.
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Tables에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!