How do you import an txt file with text, numbers, and varying columns in each row?

4 visualizaciones (últimos 30 días)
I have a txt file (extract attached) from which I need to collect all the numerical values. I have used the dlmread, readmatrix, load, and importdata functions all with no success. When using the load function:
k_input = load('Element Stiffness Matrices 1.txt');
I get the following error: "Number of columns on line 2 of ASCII file Element Stiffness Extract.txt must be the same as previous lines." When using the importdata function:
k_input = importdata('Element Stiffness Matrices 1.txt');
The textdata file formed only contains text from the first few rows of the txt file. The txt file gives stiffness matrices, outputted from FE software Abaqus. I am trying to create an autonomous code that reads this data into matlab so manual intervention will not be possible. The full file is very large (nearly 500,000 lines with over 5000 repeats of data similar to that in the extract) and can be reduced to a sixth of the size but no smaller, which would still leave a very large file.
Any help would be appreciated, thanks for your help in advance.
  8 comentarios
Alex Alex
Alex Alex el 10 de Mzo. de 2021
try function "textscan" and begin work with this information....
fileid=fopen('Element Stiffness Matrices 1.txt')
C = textscan(fileid,'%s ','Delimiter','\b','CommentStyle','*');
Mario Malic
Mario Malic el 10 de Mzo. de 2021
Editada: Mario Malic el 10 de Mzo. de 2021
I have a better question,
How do you export this file? Do you have some settings to choose? I think I have seen some files, that do not "break the line" by going into the next line, which would be helpful in your case. How big is the file, could you output the complete stiffness matrix, this would save you a lot of coding effort.

Iniciar sesión para comentar.

Respuestas (1)

Harikrishnan Balachandran Nair
Harikrishnan Balachandran Nair el 27 de Jul. de 2021
From my understanding, you are facing an issue in importing a text file, with varying number of columns in each row. The possible workaround for this would be to use the textscan function in Matlab to import the text file into your workspace.
fin = fopen( 'Element Fitness Extract.txt' );
output = textscan( fin, '%s%s%s%s', 'CollectOutput' , true, 'Delimiter', ',' );

Categorías

Más información sobre Large Files and Big Data en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by