matlabimporttext-filesdata-import

How to Import/parse sparse data from a text file into a MATLAB array?


I've been having issues with parsing data into MATLAB from a text file. The text file has discontinuities between its strings (it has spaces), and it seems like every time I tried to import the data into MATLAB it just combines everything and messes up the data. I would like to basically read the text file (attached) and import the corresponding strings with their values into a structure. I also tried to import the file into Excel and see if I could delimiter my data in a nicer format so I can easily import it into MATLAB but excel also does not like the data format and it breaks every word into a column which messes up everything as well. Any help would be greatly appreciate it.

Here is what I have so far for the code and the output is attached 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

Here is the text file (since I could not attach it):

#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

Solution

  • I was able to figure it out doing the following:

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