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

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

 Accepted Answer

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 Comments

@dpb The code works only after the second line. Using the the line written as function and the rest of the code generates no results.
@dpb Is there any chance this could be written in a 2016a version?
Thank you
The string class was introduced in R2016b, not -a, so use cellstr() instead and replace the string matching functions with equiavlent logic using strfind and/or regexp to locate the targets.
readlines and the other strings stuff like split didn't show up until R2020b so you'll also have to revert to more primitive io routines to read in the files originally as cellstr arrays. textscan can do this; there used to be an example of reading an m-file as an array of cellstr(); they seem to have removed that with the advent of newer stuff; hopefully it's still in your old release locally installed doc
Sure, it's doable, but will take some more effort without some of the newer features to do more low-level operations...
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.

Sign in to comment.

More Answers (0)

Categories

Asked:

on 7 Aug 2022

Commented:

on 1 Sep 2022

Community Treasure Hunt

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

Start Hunting!