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

4 visualizzazioni (ultimi 30 giorni)
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 Commenti

Accedi per commentare.

Risposta accettata

dpb
dpb il 17 Ago 2022
Modificato: dpb il 17 Ago 2022
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 Commenti
dpb
dpb il 26 Ago 2022
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.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Data Import and Export in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by