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

4 visualizaciones (últimos 30 días)
Bonie10
Bonie10 el 18 de Sept. de 2020
Editada: Bonie10 el 22 de Sept. de 2020
I've been having issues with parsing data into MATLAB from a text file. The textfile has discontinuities between its strings (it has spaces), and it seems like everytime 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 corresponging strings with their values into an array (probably).
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 did so far:
%% 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("test.txt", opts);
%% Clear temporary variables
clear opts

Respuesta aceptada

Mohammad Sami
Mohammad Sami el 20 de Sept. de 2020
After some testing, it seems your file can be read by setting delimiter to 3x spaces.
You can combine this with the rest of your options here as needed.
opts = detectImportOptions('test.txt','Delimiter',' ','ConsecutiveDelimitersRule','join','ExpectedNumVariables',3,'LeadingDelimitersRule','ignore');
opts = setvartype(opts,'VALUEDIMENSIONS','char');
a = readtable('test.txt',opts);
  8 comentarios
Mohammad Sami
Mohammad Sami el 22 de Sept. de 2020
I think the only other option left is to used fixed width option. This assumes that each column has fixed character width.
opts = fixedWidthImportOptions('NumVariables',4,'VariableWidths',[30 12 6 12]);
opts.VariableNames = {'DESCRIPTION' 'NAME' 'VALUE' 'DIMENSIONS'};
a = readtable('test.txt',opts);
b = readtable('test2.txt',opts);
Bonie10
Bonie10 el 22 de Sept. de 2020
Editada: Bonie10 el 22 de Sept. de 2020
That helps, thank you so much!

Iniciar sesión para comentar.

Más respuestas (1)

Walter Roberson
Walter Roberson el 21 de Sept. de 2020
S=fileread('test.txt') ;
data = str2double(regexp(S, '-?\d+(\.\d*)?, 'match' ) ;
This code allows for negative data even though the sample file does not have any.
This code allows for integers with no decimal point. The file has one of those.
This code does not allow for numbers less than 1 that do not have a leading 0 - 0.1 is fine but not .1 without the 0
This code does not allow exponential notation.
  4 comentarios
Bonie10
Bonie10 el 21 de Sept. de 2020
Editada: Bonie10 el 21 de Sept. de 2020
When running this code, the table is missing data and also the last two columns "VALUE" and "DIMENSIONS" are combined (they should be separate)... do you know why?
Bonie10
Bonie10 el 21 de Sept. de 2020
Editada: Bonie10 el 21 de Sept. de 2020
I have attached another file (test2.txt) that reflects some format changes that is more relevant to the text file I am dealing with.
Notice that this file starts with two different rows that I wouldn't want to include in my structure, do you know how to NOT include those and only parse the NAME/VALUE columns data?
Thanks!

Iniciar sesión para comentar.

Productos


Versión

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by