Now, I need help with coding part as to copy and paste the data from one file to another but section wise with the same line name in place. Thank you

6 次查看(过去 30 天)
Thanking you all in advance.
I have attached here the smaller file called .mat file and the main file named as .Main file. Both these files are in a similar format. Now, I need help with coding part as to copy and paste the data from the .mat file into the Main file such that the entire row from .mat file is copied into the main file having same row name and it has to be at the end of the row with the same name. For example, here we have CTETRA in .mat file and I would like all that data to be placed after the last line of CTETRA in the main file. Similarly for GRID, PSOLID, MAT and MATF wil be after MAT1. All details in the main file should be unchanged other than those pasted from .mat file. Also, the "end data " from the last line of .mat file needs to be ignored.
Kindly please help me in this as I am a beginner in coding with Matlab.
Thank you
  3 个评论

请先登录,再进行评论。

采纳的回答

dpb
dpb 2022-8-17
编辑:dpb 2022-8-17
Try this...looks like it worked ok, but you'll need to check carefully; I just did casual look-see.
This demands that the file format of the .mat file be as the example -- all types are one line except for the MATF bunch and they MUST be contiguous and last in the file -- the logic to pick up the continued lines would be a pain, otherwise.
function main=mergefem
main=readlines('main.fem');
addfrom=readlines('mat.fem');
SOURCE=["CTETRA","GRID","PSOLID","MAT","MATF"];
TARGET=["CTETRA","GRID","PSOLID","MAT1","MAT1"];
addfrom=addfrom(strlength(addfrom)>0);
addfrom(contains(addfrom,'ENDDATA'))=[];
for i=1:numel(SOURCE)
tgt=find(startsWith(strtrim(main),TARGET(i)),1,'last'); % location of end of section in original file
if matches(SOURCE(i),"MATF") % got to deal with multiple lines
src=find(startsWith(strtrim(addfrom),SOURCE(i)),1); % location in source file
grab=addfrom(src:end); % multiple lines -- must be last section in .mat file
else
grab=addfrom(startsWith(strtrim(addfrom),SOURCE(i))); % each one line begins with SOURCE string
end
front=main(1:tgt); % beginning to last of the target section
back=main(tgt+1:end); % and the rest
main=[front;grab;back]; % insert wanted section
end
end
Obviously, it would be more general to pass in the object filenames as variables or read the files externally and pass the arrays; "salt to suit!"
  5 个评论
dpb
dpb 2022-8-26
The example I was thinking about is still in the doc for the venerable textread instead of textscan; maybe it never was moved over...but it gives the magic incantation --
main=textread('main.fem','%s','delimiter','\n','whitespace','');
will replace readlines with a cellstr array instead of a string array. You may get the deprecated warning message in code editor with it; the approved "modern" substitute is textscan but it requires explicitly opening/closing the file handle and another conversion from the composite cell returned--
fid=fopen('main.fem','r');
main=textscan(fid, '%s', 'delimiter', '\n','whitespace','');
main=main{1};
fid=fclose(fid);
is same cellstr array.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Large Files and Big Data 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by