How can I convert all string cells in a table to numerical values?

조회 수: 16(최근 30일)
Ted Baker
Ted Baker 2020년 1월 6일
답변: Stijn Haenen 2020년 1월 6일
Hi I'm trying to import a text file into a table format. The text file is separated by single spaces, which I have included as an attachment. However, several columns have the data wrapped in a quote mark - why is this and what can I do to remove them? My matlab code looks as follows:
filename = 'test.txt';
T = readtable(filename,'Delimiter',' ','ReadVariableNames',false);
T([1],:) = [];
size(T);
EDIT: I see now the quote marks represent a string in the cell, but how can I convert all the strings to numerical values?

채택된 답변

Stijn Haenen
Stijn Haenen 2020년 1월 6일
If you use the import data tool, you can chose to ouput the data in matrix instead of a table.
With this tool you can also generate a script to import your data:
opts = delimitedTextImportOptions("NumVariables", 9);
% Specify range and delimiter
opts.DataLines = [7, Inf];
opts.Delimiter = " ";
% Specify column names and types
opts.VariableNames = ["Agilent", "TechnologiesE8362BMY43020256A060432", "VarName3", "VarName4", "VarName5", "VarName6", "VarName7", "VarName8", "VarName9"];
opts.VariableTypes = ["double", "double", "double", "double", "double", "double", "double", "double", "double"];
% Specify file level properties
opts.ExtraColumnsRule = "ignore";
opts.EmptyLineRule = "read";
opts.ConsecutiveDelimitersRule = "join";
opts.LeadingDelimitersRule = "ignore";
% Import the data
test = readtable("D:\thuis\test.txt", opts);
%% Convert to output type
test = table2array(test);
%% Clear temporary variables
clear opts

추가 답변(0개)

태그

Community Treasure Hunt

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

Start Hunting!

Translated by