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
Walter Roberson
Walter Roberson 2021 年 1 月 18 日
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 .
Walter Roberson
Walter Roberson 2021 年 1 月 18 日
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 ExchangeData 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