Effacer les filtres
Effacer les filtres

Re-organize LS-DYNA data from text file removing header and footer

10 vues (au cours des 30 derniers jours)
Hello,
I'm trying to extract data from an LS-DYNA output text file and write it into a new file with a new format, but I find the initial format a bit complicated and I have problem getting what I want, which is:
  • Header: keep the same (LS-DYNA keyword ... date)
  • One line with the legend of the data, with timestep and time moved to columns 1 and 2, respectively (two new columns should be created)
  • N lines with the actual data
  • No footer
The starting file looks like this:
and I'd like to have it like this
From that point I can use textscan to manipulate the data the way I want.
Numpy library in Python has the command genfromtxt that allows to remove header and footer, then choose which data to extract from a specific column of a specific line. I couln't find such a command in MATLAB, and i tried with a combination of, fgets, fgetl, and textscan, but impossible to achieve what I'm looking for.
Does anyone have an idea of which function would work best?
Thanks a lot!

Réponse acceptée

Sam McDonald
Sam McDonald le 25 Oct 2016
I understand that you would like to know what MATLAB functions would work best to format your data so it can be easily imported using textscan.
Because your original data file is not formatted the same on each line, you will likely need to call 'fgetl' to read in each line as a character array. Use 'eof' to determine when the end of the file has been reached.
To extract the numerical values from lines that contain many characters, you can first remove the characters by using the 'replace' function.
Then, you can use 'textscan' on the remaining string to extract the numerical values.
Once you have read the data into MATLAB, you can use a function like 'fprintf' to write the data to another file.
These functions should help you write custom code to achieve the desired format.
  1 commentaire
Davide De Cicco
Davide De Cicco le 27 Oct 2016
Thanks a lot for your answer.
I tried the replace function but I couldn't really understand how to use it. So I decided to go with sscanf instead. In fact, in the documentation, there is an example that shows how to extract only the numbers when there are different formats in a string. And it works pretty much in the same way as textscan (or I just didn't understand the difference between them).
I ended up with a code that gives me what I need (even if it is probably possible to optimize it). Here it is, in case it might be useful to someone else:
function numsTot = transfNodout(resufile, ext)
%--------------------
% Read data from file
%--------------------
% Open the file
fid3 = fopen(resufile,'r');
% Read the header
header = 5; % nb of header lines
for line = 1:header
tline3 = fgetl(fid3);
head{line} = tline3;
end
% Loop to read the NODOUT data
line = 1;
while ischar(tline3)
tline3 = fgetl(fid3);
formatSpec = [repmat('%*s ',[1,24]) '%f %*s %*s %*s %f %*s']
nums1(line,:) = sscanf(tline3, formatSpecs, [1, inf]);
for j = 1:3
tline3 = fgetl(fid3);
end
nums2(line,:) = sscanf(tline3, '%f');
for j = 1:3
tline3 = fgetl(fid3);
end
numsTot(line,:) = [nums1(line,:) nums2(line,:)];
line = line+1;
end
% Close the file
fclose(fid3);
% Get the legend
fid3 = fopen(resufile,'r');
lin_legend = 8; % line where the legend is
for line = 1:lin_legend
tline3 = fgetl(fid3);
legend{1} = tline3;
end
fclose(fid3);
%---------------------
% Write data into file
%---------------------
% Write the header
newname = [ext resufile];
fid4 = fopen(newname', 'w');
for line = 1:header
fprintf(fid4,'%s\n', head{line});
end
% Write the legend, with addition of 2 columns
fprintf(fid4, ' time step time ');
fprintf(fid4, '%s\n', legend{1});
% Write the data
formatSpec = [repmat('%E ',[1,14]) '%f\n'];
for line = 1:size(numsTot,1)
fprintf(fid4, formatSpec, numsTot(line,:));
end
fclose(fid4);
end

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by