MATLAB Answers

Edit Textfile for analysis

30 views (last 30 days)
RAKESH KUMAR TOTA
RAKESH KUMAR TOTA on 12 Jan 2021
Edited: Rik on 18 Jan 2021
Hi, I here by attached text file. I want the text file to be read as the following manner. Any help would be appreciated. Thank you
*Heading
** Job name: Job-1 Model name: Model-1
** Generated by: Abaqus/CAE 6.14-5
*Preprint, echo=NO, model=NO, history=NO, contact=NO
**
** PARTS
**
*Part, name=rect
*Node
1, -0.375, -0.125
2, -0.427631587, -0.125
3, -0.480263144, -0.125
*Element, type=CPS4
1, 1, 2, 22, 21
2, 2, 3, 23, 22
3, 3, 4, 24, 23
4, 4, 5, 25, 24
*End Part
**

  2 Comments

Mathieu NOE
Mathieu NOE on 13 Jan 2021
hi
you can use readlines :
readlines('file.txt')
RAKESH KUMAR TOTA
RAKESH KUMAR TOTA on 13 Jan 2021
Please check the text fie which i uploaded. I know how to read the file but the problem is i have to edit the node data lines where . to be remove and also one line to be leaved after starting element data and also after element data one line to be leaved. All lines to be started from left allignemnt and there should be a gap of only one space between node and element data.
any help in this direction would be appreciated

Sign in to comment.

Accepted Answer

Rik
Rik on 13 Jan 2021
Edited: Rik on 13 Jan 2021
You can get my readfile function from the FEX. If you are using R2017a or later, you can also get it through the AddOn-manager.
The readlines function was introduced in R2020b and will work similar to readfile (except it will read to a string instead of a cell array of char arrays).
data=readfile('https://www.mathworks.com/matlabcentral/answers/uploaded_files/486763/aba.txt');
if isempty(data{end}),data(end)=[];end %strip trailing empty line if the file ended with a newline
for n=1:numel(data)
if ~strcmp(data{n}(1),'*')
str=data{n};
%trim leading/trailing whitespace
str=strtrim(str);
%trim internal double spaces
removed_double_spaces=inf;
while removed_double_spaces~=0
length1=length(str);
str=strrep(str,' ',' ');
length2=length(str);
removed_double_spaces=length1-length2;
end
%store back to cell array
data{n}=str;
end
end
fprintf('%s\n',data{:})
*Heading ** Job name: Job-1 Model name: Model-1 ** Generated by: Abaqus/CAE 6.14-5 *Preprint, echo=NO, model=NO, history=NO, contact=NO ** ** PARTS ** *Part, name=rect *Node 1, -0.375, -0.125 2, -0.427631587, -0.125 3, -0.480263144, -0.125 *Element, type=CPS4 1, 1, 2, 22, 21 2, 2, 3, 23, 22 3, 3, 4, 24, 23 4, 4, 5, 25, 24 *End Part **

  6 Comments

Show 3 older comments
Rik
Rik on 13 Jan 2021
And what do you want the resulting file to be? Both readfile and readlines will work on the file you uploaded.
RAKESH KUMAR TOTA
RAKESH KUMAR TOTA on 13 Jan 2021
Please check the allignment of lines in the text file. I want to change the text file according to the above way. Please see the above line which i wrote in comment section and check it with the text file . There is variation. For example please see the 10th line of text fiel and line which i would like to in text fiel i posted. Thank you
RAKESH KUMAR TOTA
RAKESH KUMAR TOTA on 18 Jan 2021
Thank you for your answer.
Can you please look the below question. Its a similar question extension. Any help would save a lot of time. Thank you in advance.

Sign in to comment.

More Answers (1)

Mathieu NOE
Mathieu NOE on 13 Jan 2021
ok
so my 2 cent suggestion to make that array looks nicer -
lines = readlines('aba.txt','WhitespaceRule','trim');
ll_out = strings; % init string array
for ci = 1:numel(lines)
ll = lines(ci,:); % current line
if ~startsWith(ll,'*'); % check lines 10 to 20 - here we have to work things out
lldk = strrep(ll,' ',''); % remove all (multiple) blanks inside the char array
ll_out(ci,1) = strrep(lldk,',',', '); % put back one blank after the comma (if really needed)
else % nothing to do except copy the original line
ll_out(ci,1) = ll;
end
end
% save it - and remove the double quote
writematrix(ll_out, 'test.txt',"QuoteStrings",0)

  1 Comment

RAKESH KUMAR TOTA
RAKESH KUMAR TOTA on 18 Jan 2021
Thank you for your answer.

Sign in to comment.

Tags

Products

Community Treasure Hunt

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

Start Hunting!

Translated by