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 Kommentare

Mario Malic
Mario Malic am 18 Jan. 2021
What is the actual question here?
RAKESH KUMAR TOTA
RAKESH KUMAR TOTA am 18 Jan. 2021
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.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Walter Roberson
Walter Roberson am 18 Jan. 2021

0 Stimmen

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 Kommentare

RAKESH KUMAR TOTA
RAKESH KUMAR TOTA am 18 Jan. 2021
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' }));
Walter Roberson
Walter Roberson am 18 Jan. 2021
Bearbeitet: Walter Roberson am 18 Jan. 2021
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.
RAKESH KUMAR TOTA
RAKESH KUMAR TOTA am 18 Jan. 2021
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
RAKESH KUMAR TOTA
RAKESH KUMAR TOTA am 18 Jan. 2021
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');
RAKESH KUMAR TOTA
RAKESH KUMAR TOTA am 18 Jan. 2021
Thanks a lot . It really saved a lot of time.
RAKESH KUMAR TOTA
RAKESH KUMAR TOTA am 18 Jan. 2021
eldata (:, 1) = 1; % why ??? ... I need to assign materail no.1 to all elements in my code. Thank you

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Data Import and Analysis finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by