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

9 ビュー (過去 30 日間)
Ted Baker
Ted Baker 2020 年 1 月 6 日
コメント済み: Stephen23 2024 年 1 月 18 日
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?
  1 件のコメント
Stephen23
Stephen23 2024 年 1 月 18 日
Note that you could easily use READMATRIX for that data file:
format long G
M = readmatrix('test.txt', 'NumHeaderLines',6)
M = 11×9
1.0e+00 * 690000000 -0.07159581 -0.8339922 -0.1265173 -0.01405961 -0.125782 -0.01377937 0.2477304 -0.920722 690193750 0.07831148 -0.7403554 -0.08229134 0.0641926 -0.08235301 0.06380991 -0.000491137 -0.8711369 690387500 -0.02860679 -0.7102471 0.07023682 0.0898001 0.07096643 0.0912611 0.5155896 -0.6735609 690581250 0.06169727 -0.8436321 -0.01743717 0.04197023 -0.01737786 0.04100154 0.3686155 -0.8648374 690775000 -0.03250416 -0.8290954 0.02440995 0.06105115 0.02438955 0.06043163 0.3282914 -0.9570565 690968750 -0.03318164 -0.7611125 0.0895226 0.137869 0.08931927 0.1369279 0.2230548 -0.8250987 691162500 -0.00651951 -0.704771 0.1610863 -0.02032301 0.161159 -0.01911074 0.2983101 -0.8181552 691356250 0.02296278 -0.8168985 0.05552047 -0.09635511 0.05485816 -0.09586232 0.2789877 -0.7758706 691550000 0.009161192 -0.7716845 0.0200344 -0.05287114 0.01965514 -0.05153497 0.4261074 -0.8412111 691743750 -0.08785366 -0.7999797 0.04284034 -0.01224851 0.04246143 -0.01270002 0.2247821 -0.9183822

サインインしてコメントする。

採用された回答

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 件)

カテゴリ

Help Center および File ExchangeText Files についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by