如何 Import/parse 将文本文件中的数据稀疏到 MATLAB 数组中?
How to Import/parse sparse data from a text file into a MATLAB array?
我在将数据从文本文件解析到 MATLAB 时遇到了问题。文本文件在其字符串之间存在不连续性(它有空格),似乎每次我尝试将数据导入 MATLAB 时它只是组合所有内容并弄乱数据。我想基本上读取文本文件(附件)并将相应的字符串及其值导入到结构中。
我还尝试将文件导入 Excel 并查看是否可以以更好的格式分隔我的数据,以便我可以轻松地将其导入 MATLAB 但 excel 也不喜欢数据格式并且它会破坏每个将单词放入一列中,这也会弄乱所有内容。
任何帮助将不胜感激。
这是我到目前为止的代码,附上输出 here:
#Setup the Import Options and import the data
opts = delimitedTextImportOptions("NumVariables", 3);
% Specify range and delimiter
opts.DataLines = [2, Inf];
opts.Delimiter = ",";
% Specify column names and types
opts.VariableNames = ["TITLE", "BEGININPUTDATAECHO", "VarName3"];
opts.VariableTypes = ["string", "string", "string"];
% Specify file level properties
opts.ExtraColumnsRule = "ignore";
opts.EmptyLineRule = "read";
% Specify variable properties
opts = setvaropts(opts, ["TITLE", "BEGININPUTDATAECHO", "VarName3"], "WhitespaceRule", "preserve");
opts = setvaropts(opts, ["TITLE", "BEGININPUTDATAECHO", "VarName3"], "EmptyFieldRule", "auto");
% Import the data
ATR42500zjf2 = readtable("file", opts);
%% Clear temporary variables
clear opts
这是文本文件(因为我无法附加它):
#NAMELIST $KDSD
ASDCSD, ASCDDF, HFSDCA AND CASDEC SASDW
DESCRIPTION NAME VALUE DIMENSIONS
A11 DATA
ABC CAD AAS ASDF SCAS 0.0000
WEFADFSA FASD GRSA FDS 3.3454
FAS FASF FWEQ EDASA 1.2534
FWE FWEFWEFW FWEF SEFEFEW 984.0 DEG
EW FWE VED FEW VWE GEWEEW 0.0 DEG
VWE WE WE WEFEWFWEFWE WEERQ 0
EWF GRWDOSS WEWE WER 1.000
SDFSFS SDFSDF VDSF SDFS 4.600
A12 DATA
ASDASD DCASH 1.0000 LBS
SDVDS VSD VSD ASCX CASS 0.00 M
SDAFSD ASFDS 23.53 M
SDFSDF BSDFVSDA FSDAF GACFEA 0.3330
JYTHHFG FG JDFGBDFDV STQSEW 0.4575
SDFDS SDFSDF FDSC HFDB SVDSDVS 2.0000
CZXCC DVDSSAD FBVSDS GRQASW 0.0000
A13 DATA
ASDD VSDV 1.0000 M
ASDAS ASFEW ASDSDA VSDA 6.23 LBS
ASDF RWFSD SDFV 8.44
AASFS GBSDS GSDF 0.5597
TASD SERT 0.4554
POAIS ADAS OJENS PASKDM SDFD 5.0000
我通过以下操作解决了这个问题:
opts = fixedWidthImportOptions('NumVariables',4,'VariableWidths',[30 12 6 12]);
opts.VariableNames = {'DESCRIPTION' 'NAME' 'VALUE' 'DIMENSIONS'};
opts.DataLines = [2 Inf];
a = readtable('test.txt',opts);
data_struct = struct(a);
我在将数据从文本文件解析到 MATLAB 时遇到了问题。文本文件在其字符串之间存在不连续性(它有空格),似乎每次我尝试将数据导入 MATLAB 时它只是组合所有内容并弄乱数据。我想基本上读取文本文件(附件)并将相应的字符串及其值导入到结构中。 我还尝试将文件导入 Excel 并查看是否可以以更好的格式分隔我的数据,以便我可以轻松地将其导入 MATLAB 但 excel 也不喜欢数据格式并且它会破坏每个将单词放入一列中,这也会弄乱所有内容。 任何帮助将不胜感激。
这是我到目前为止的代码,附上输出 here:
#Setup the Import Options and import the data
opts = delimitedTextImportOptions("NumVariables", 3);
% Specify range and delimiter
opts.DataLines = [2, Inf];
opts.Delimiter = ",";
% Specify column names and types
opts.VariableNames = ["TITLE", "BEGININPUTDATAECHO", "VarName3"];
opts.VariableTypes = ["string", "string", "string"];
% Specify file level properties
opts.ExtraColumnsRule = "ignore";
opts.EmptyLineRule = "read";
% Specify variable properties
opts = setvaropts(opts, ["TITLE", "BEGININPUTDATAECHO", "VarName3"], "WhitespaceRule", "preserve");
opts = setvaropts(opts, ["TITLE", "BEGININPUTDATAECHO", "VarName3"], "EmptyFieldRule", "auto");
% Import the data
ATR42500zjf2 = readtable("file", opts);
%% Clear temporary variables
clear opts
这是文本文件(因为我无法附加它):
#NAMELIST $KDSD
ASDCSD, ASCDDF, HFSDCA AND CASDEC SASDW
DESCRIPTION NAME VALUE DIMENSIONS
A11 DATA
ABC CAD AAS ASDF SCAS 0.0000
WEFADFSA FASD GRSA FDS 3.3454
FAS FASF FWEQ EDASA 1.2534
FWE FWEFWEFW FWEF SEFEFEW 984.0 DEG
EW FWE VED FEW VWE GEWEEW 0.0 DEG
VWE WE WE WEFEWFWEFWE WEERQ 0
EWF GRWDOSS WEWE WER 1.000
SDFSFS SDFSDF VDSF SDFS 4.600
A12 DATA
ASDASD DCASH 1.0000 LBS
SDVDS VSD VSD ASCX CASS 0.00 M
SDAFSD ASFDS 23.53 M
SDFSDF BSDFVSDA FSDAF GACFEA 0.3330
JYTHHFG FG JDFGBDFDV STQSEW 0.4575
SDFDS SDFSDF FDSC HFDB SVDSDVS 2.0000
CZXCC DVDSSAD FBVSDS GRQASW 0.0000
A13 DATA
ASDD VSDV 1.0000 M
ASDAS ASFEW ASDSDA VSDA 6.23 LBS
ASDF RWFSD SDFV 8.44
AASFS GBSDS GSDF 0.5597
TASD SERT 0.4554
POAIS ADAS OJENS PASKDM SDFD 5.0000
我通过以下操作解决了这个问题:
opts = fixedWidthImportOptions('NumVariables',4,'VariableWidths',[30 12 6 12]);
opts.VariableNames = {'DESCRIPTION' 'NAME' 'VALUE' 'DIMENSIONS'};
opts.DataLines = [2 Inf];
a = readtable('test.txt',opts);
data_struct = struct(a);