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

Mario Malic
Mario Malic 2021년 1월 18일
What is the actual question here?
RAKESH KUMAR TOTA
RAKESH KUMAR TOTA 2021년 1월 18일
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.

댓글을 달려면 로그인하십시오.

 채택된 답변

Walter Roberson
Walter Roberson 2021년 1월 18일

0 개 추천

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

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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Data Import and Analysis에 대해 자세히 알아보기

제품

릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by