How to load text file and extract number from file that is not organized into columns?

1 visualización (últimos 30 días)
I am running a finite element analyzer, that deposits the results into a text file called DYNA_1, DYNA_2, ect for each load step. By the end of the run there are thousands of these files, and I would like to get a certain number from each into an array in Matlab for processing.
I have attached one such file for example. When you open it, you will see the information is mixed text and numbers, and not aligned in orderly columns. This file is the output file for many elements and nodes, and I would like to learn how to extract a certain number, dx for node 885, for example.
Please help
Sincerely

Respuesta aceptada

dpb
dpb el 19 de En. de 2019
Editada: dpb el 19 de En. de 2019
That's a very nicely organized file in the sections...you just have to know what the section headers are and which you want...
A very crude outline of one way to do for a particular case--
fid=fopen('DYNA_10.txt'); % open the file
l=fgetl(fid); % get first line, initialize buffer
while ~strcmp(l,'DISPLACEMENTS') % find the section header
l=strtrim(fgetl(fid));
if ~ischar(l),error('Section not found'),break,end % break infinite loop if something goes wrong
end
displ=cell2mat(textscan(fid,repmat('%f',1,6),'HeaderLines',5,'CollectOutput',1)); % return the data in array
fid=fclose(fid); % close file
This returns
>> whos displ
Name Size Bytes Class Attributes
displ 443x6 21264 double
>> displ(1:5,:)
ans =
1.0000 0 0 2.0000 0 0
3.0000 0 0 4.0000 0 0
5.0000 -0.0030 -0.0020 6.0000 -0.0030 -0.0020
7.0000 -0.0020 -0.0050 8.0000 -0.0010 -0.0070
9.0000 -0.0020 -0.0010 10.0000 -0.0030 -0.0040
>> displ(end,:)
ans =
885.0000 0.3770 -1.8090 NaN NaN NaN
>>
Obviously you can return whatever particular node you wish by lookup into the first/fourth columns for a value or by computing the actual line number/location from the fact there are two nodes/record in the file. (Of course, you could make that calculation after finding the header record and compute the 'headerlines' value and only read a given record, too).
For general use, wrap the above logic in a function that can be called with a specific section ID string and save a lookup table of what the format is within each section (or dispatch a section-specific function for each section in a CASE construct or the like).
It would then be trivial to simply wrap all that in a loop on the files returned by a dir over the appropariate wildcard string like
d=dir(fullfile(workingdirectory,'DYN*.txt'));
for i=1:numel(d)
...
end

Más respuestas (0)

Categorías

Más información sobre Text Data Preparation en Help Center y File Exchange.

Productos


Versión

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by