Hi, Good morning
I have an input file input.txt, i need to save the format in different two files node.txt and el.txt in the format attached files. Any help would be appreciated . Thank you inadvance.

2 Comments

What is the actual question here?
i need to extract data from input.txt file ( nodes and element data) and save in the format node.txt and el.txt as mentioned in the uploaded files. Thank you.

Sign in to comment.

 Accepted Answer

fid = fopen('input.txt', 'r');
nodedata = cell2mat( textscan(fid, '%*f,%f,%f', 'CommentStyle', {'*Heading', '*Node'}) );
eldata = cell2mat( textscan(fid, '%*f,%f,%f,%f,%f', 'HeaderLines', 1) );
fclose(fid)

8 Comments

Error using textscan
Unable to parse the format character vector at position 1 ==> % * f,% f,% f
Unsupported format specifier '% '. See the documentation for TEXTSCAN for supported formats.
Error in inputnodeel (line 2)
nodedata = cell2mat (textscan (fid, '% * f,% f,% f' , 'CommentStyle' , { '* Heading' , '*
Node' }));
The format I gave is % followed immediately by * followed immediately by f. Somehow you intepreted it as % followed by space followed by * followed by space followed by f.
At the upper right of the block of code I posted, there is a rectangle that shows a series of white lines. Click on that and my code will be added to your clipboard. You can then paste the code into your program.
I will mark all the places that space occurred in my first line:
nodedata = cell2mat( textscan(fid, '%*f,%f,%f', 'CommentStyle', {'*Heading', '*Node'}) );
^ ^ ^ ^ ^ ^ ^ ^
all of those spaces are optional. You could use the code
fid=fopen('input.txt','r');
nodedata=cell2mat(textscan(fid,'%*f,%f,%f','CommentStyle',{'*Heading','*Node'}));
eldata=cell2mat(textscan(fid,'%*f,%f,%f,%f,%f','HeaderLines',1));
fclose(fid);
which has absolutely no spaces in it.
It generated four variables as output,
fid, ans =0 , eldata= [] (0*4 double), node datat = [] (0*2 double). This is for your kind information.I need to save it two text files node.txt and el.txt from input.txt. Thank you
The code was tested with the version that you indicate that you are running.
fid=fopen('input.txt','r');
nodedata=cell2mat(textscan(fid,'%*f,%f,%f','CommentStyle',{'*Heading','*Node'}));
eldata=cell2mat(textscan(fid,'%f,%f,%f,%f,%f','HeaderLines',1));
fclose(fid);
size(nodedata)
ans = 1×2
451 2
size(eldata)
ans = 1×2
400 5
writematrix(eldata, 'el.txt');
writematrix(nodedata, 'node.txt');
!wc -l el.txt
400 el.txt
!wc -l node.txt
451 node.txt
Thank you for your code. But there are slight modifications to be needed for the code . There should not be any commas between node data and element data in each line only one character space between data in each line and also first column of el data is 1 in all lines of el.txt. could you please do it. I am happy for your work. Thank you .
fid=fopen('input.txt','r');
nodedata=cell2mat(textscan(fid,'%*f,%f,%f','CommentStyle',{'*Heading','*Node'}));
eldata=cell2mat(textscan(fid,'%f,%f,%f,%f,%f','HeaderLines',1));
fclose(fid);
eldata(:,1)=1;%why???
writematrix(eldata,'el.txt','delimiter','space');
writematrix(nodedata,'node.txt','delimiter','space');
Thanks a lot . It really saved a lot of time.
eldata (:, 1) = 1; % why ??? ... I need to assign materail no.1 to all elements in my code. Thank you

Sign in to comment.

More Answers (0)

Categories

Find more on Data Import and Analysis in Help Center and File Exchange

Products

Release

R2020b

Community Treasure Hunt

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

Start Hunting!