How to exported formatted text and create files in array with specified variables ?

Is there possible to export formated text shown below (from %start line to export to %end line to export) ? In addition, i want to export/create thoose lines to file but it creates in array with different 'variant model name'
So theese are the result files that i want to :
'ModelA.$2k' (or it may be .txt extension) (contains text from line 6 to line 22)
'ModelB.$2k' (or it may be .txt extension) (contains text from line 6 to line 22)
'ModelC.$2k' (or it may be .txt extension) (contains text from line 6 to line 22)
'ModelD.$2k' (or it may be .txt extension) (contains text from line 6 to line 22)
'ModelE.$2k' (or it may be .txt extension) (contains text from line 6 to line 22)
is it possible ? or do you have other way to this ? actually this formated text was from text-file with .txt extension. But i want to create few files which have different contents in each files.

 Réponse acceptée

There are several ways to do this —
VariantModel = compose('%c', 'A':'E')
VariantModel = 1x5 cell array
{'A'} {'B'} {'C'} {'D'} {'E'}
files = sprintf('''Model%c.$2k'' (or it may be .txt extension) (contains text from line 6 to line 22)\n',VariantModel{:})
files =
''ModelA.$2k' (or it may be .txt extension) (contains text from line 6 to line 22) 'ModelB.$2k' (or it may be .txt extension) (contains text from line 6 to line 22) 'ModelC.$2k' (or it may be .txt extension) (contains text from line 6 to line 22) 'ModelD.$2k' (or it may be .txt extension) (contains text from line 6 to line 22) 'ModelE.$2k' (or it may be .txt extension) (contains text from line 6 to line 22) '
Another option is to use compose to create a cell array of them —
files = compose('''Model%c.$2k'' (or it may be .txt extension) (contains text from line 6 to line 22)','A':'E');
files{:}
ans = ''ModelA.$2k' (or it may be .txt extension) (contains text from line 6 to line 22)'
ans = ''ModelB.$2k' (or it may be .txt extension) (contains text from line 6 to line 22)'
ans = ''ModelC.$2k' (or it may be .txt extension) (contains text from line 6 to line 22)'
ans = ''ModelD.$2k' (or it may be .txt extension) (contains text from line 6 to line 22)'
ans = ''ModelE.$2k' (or it may be .txt extension) (contains text from line 6 to line 22)'
Then for each file:
filename = files{1}
filename = ''ModelA.$2k' (or it may be .txt extension) (contains text from line 6 to line 22)'
And so for the others.
EDIT — Added ‘for each file’ example at the end.
.

6 commentaires

i mean, i want to have files like this :
File number 1: ModelA.$2k
%inside the file are:
File "D:\REF FAROS\Faros\S2k Model List\ModelA.$2k was saved on m/d/yy at h:mm:ss"
TABLE: "ACTIVE DEGREES OF FREEDOM"
UX=Yes UY=No UZ=Yes RX=No RY=Yes RZ=No
TABLE: "ANALYSIS OPTIONS"
Solver=Advanced SolverProc=Auto Force32Bit=No StiffCase=None GeomMod=None HingeOpt="In Elements" NumAThreads=0 MaxFileSize=0 NumDThreads=0 NumRThreads=0 UseMMFiles="Program Determined" AllowDiff=No
TABLE: "AUTO COMBINATION OPTION DATA 01 - GENERAL"
DesignType=Steel AutoGen=No
TABLE: "AUTO WAVE 3 - WAVE CHARACTERISTICS - GENERAL"
WaveChar=Default WaveType="From Theory" KinFactor=1 SWaterDepth=45 WaveHeight=18 WavePeriod=12 WaveTheory=Linear
TABLE: "CASE - MODAL 1 - GENERAL"
Case=MODAL ModeType=Eigen MaxNumModes=12 MinNumModes=1 EigenShift=0 EigenCutoff=0 EigenTol=1E-09 AutoShift=Yes
File number 2: ModelB.$2k
%inside the file are:
File "D:\REF FAROS\Faros\S2k Model List\ModelB.$2k was saved on m/d/yy at h:mm:ss"
TABLE: "ACTIVE DEGREES OF FREEDOM"
UX=Yes UY=No UZ=Yes RX=No RY=Yes RZ=No
TABLE: "ANALYSIS OPTIONS"
Solver=Advanced SolverProc=Auto Force32Bit=No StiffCase=None GeomMod=None HingeOpt="In Elements" NumAThreads=0 MaxFileSize=0 NumDThreads=0 NumRThreads=0 UseMMFiles="Program Determined" AllowDiff=No
TABLE: "AUTO COMBINATION OPTION DATA 01 - GENERAL"
DesignType=Steel AutoGen=No
TABLE: "AUTO WAVE 3 - WAVE CHARACTERISTICS - GENERAL"
WaveChar=Default WaveType="From Theory" KinFactor=1 SWaterDepth=45 WaveHeight=18 WavePeriod=12 WaveTheory=Linear
TABLE: "CASE - MODAL 1 - GENERAL"
Case=MODAL ModeType=Eigen MaxNumModes=12 MinNumModes=1 EigenShift=0 EigenCutoff=0 EigenTol=1E-09 AutoShift=Yes
File number 3: ModelC.$2k
%inside the file are:
File "D:\REF FAROS\Faros\S2k Model List\ModelC.$2k was saved on m/d/yy at h:mm:ss"
TABLE: "ACTIVE DEGREES OF FREEDOM"
UX=Yes UY=No UZ=Yes RX=No RY=Yes RZ=No
TABLE: "ANALYSIS OPTIONS"
Solver=Advanced SolverProc=Auto Force32Bit=No StiffCase=None GeomMod=None HingeOpt="In Elements" NumAThreads=0 MaxFileSize=0 NumDThreads=0 NumRThreads=0 UseMMFiles="Program Determined" AllowDiff=No
TABLE: "AUTO COMBINATION OPTION DATA 01 - GENERAL"
DesignType=Steel AutoGen=No
TABLE: "AUTO WAVE 3 - WAVE CHARACTERISTICS - GENERAL"
WaveChar=Default WaveType="From Theory" KinFactor=1 SWaterDepth=45 WaveHeight=18 WavePeriod=12 WaveTheory=Linear
TABLE: "CASE - MODAL 1 - GENERAL"
Case=MODAL ModeType=Eigen MaxNumModes=12 MinNumModes=1 EigenShift=0 EigenCutoff=0 EigenTol=1E-09 AutoShift=Yes
etc.
It took me a while to figure out what you want to do. Except for the first line, they look to be all the same (I did not go through them in detail).
Try this —
VariantModel = compose('%c', 'A':'E') % Multiple Files Creation
VariantModel = 1x5 cell array
{'A'} {'B'} {'C'} {'D'} {'E'}
for k = 1:numel(VariantModel)
writefile(VariantModel{k})
end
files = dir('*.$2k');
files(:).name
ans = 'ModelA.$2k'
ans = 'ModelB.$2k'
ans = 'ModelC.$2k'
ans = 'ModelD.$2k'
ans = 'ModelE.$2k'
type('ModelE.$2k')
File "D:\REF FAROS\Faros\S2k Model List\ModelE.$2k was saved on m/d/yy at h:mm:ss" TABLE: "ACTIVE DEGREES OF FREEDOM" UX=Yes UY=No UZ=Yes RX=No RY=Yes RZ=No TABLE: "ANALYSIS OPTIONS" Solver=Advanced SolverProc=Auto Force32Bit=No StiffCase=None GeomMod=None HingeOpt="In Elements" NumAThreads=0 MaxFileSize=0 NumDThreads=0 NumRThreads=0 UseMMFiles="Program Determined" AllowDiff=No TABLE: "AUTO COMBINATION OPTION DATA 01 - GENERAL" DesignType=Steel AutoGen=No TABLE: "AUTO WAVE 3 - WAVE CHARACTERISTICS - GENERAL" WaveChar=Default WaveType="From Theory" KinFactor=1 SWaterDepth=45 WaveHeight=18 WavePeriod=12 WaveTheory=Linear TABLE: "CASE - MODAL 1 - GENERAL" Case=MODAL ModeType=Eigen MaxNumModes=12 MinNumModes=1 EigenShift=0 EigenCutoff=0 EigenTol=1E-09 AutoShift=Yes
Char = 'A';
writefile(Char) % Single File Creation
type('ModelA.$2k')
File "D:\REF FAROS\Faros\S2k Model List\ModelA.$2k was saved on m/d/yy at h:mm:ss" TABLE: "ACTIVE DEGREES OF FREEDOM" UX=Yes UY=No UZ=Yes RX=No RY=Yes RZ=No TABLE: "ANALYSIS OPTIONS" Solver=Advanced SolverProc=Auto Force32Bit=No StiffCase=None GeomMod=None HingeOpt="In Elements" NumAThreads=0 MaxFileSize=0 NumDThreads=0 NumRThreads=0 UseMMFiles="Program Determined" AllowDiff=No TABLE: "AUTO COMBINATION OPTION DATA 01 - GENERAL" DesignType=Steel AutoGen=No TABLE: "AUTO WAVE 3 - WAVE CHARACTERISTICS - GENERAL" WaveChar=Default WaveType="From Theory" KinFactor=1 SWaterDepth=45 WaveHeight=18 WavePeriod=12 WaveTheory=Linear TABLE: "CASE - MODAL 1 - GENERAL" Case=MODAL ModeType=Eigen MaxNumModes=12 MinNumModes=1 EigenShift=0 EigenCutoff=0 EigenTol=1E-09 AutoShift=Yes
function writefile(C)
fido = fopen("Model"+C+".$2k",'wt');
fprintf(fido,'File "D:\\REF FAROS\\Faros\\S2k Model List\\Model%c.$2k was saved on m/d/yy at h:mm:ss"\n\n TABLE: "ACTIVE DEGREES OF FREEDOM"\n\n UX=Yes UY=No UZ=Yes RX=No RY=Yes RZ=No\n\n TABLE: "ANALYSIS OPTIONS"\n\n Solver=Advanced SolverProc=Auto Force32Bit=No StiffCase=None GeomMod=None HingeOpt="In Elements" NumAThreads=0 MaxFileSize=0 NumDThreads=0 NumRThreads=0 UseMMFiles="Program Determined" AllowDiff=No\n\n TABLE: "AUTO COMBINATION OPTION DATA 01 - GENERAL"\n\n DesignType=Steel AutoGen=No\n\n TABLE: "AUTO WAVE 3 - WAVE CHARACTERISTICS - GENERAL"\n\n WaveChar=Default WaveType="From Theory" KinFactor=1 SWaterDepth=45 WaveHeight=18 WavePeriod=12 WaveTheory=Linear\n\n TABLE: "CASE - MODAL 1 - GENERAL"\n\n Case=MODAL ModeType=Eigen MaxNumModes=12 MinNumModes=1 EigenShift=0 EigenCutoff=0 EigenTol=1E-09 AutoShift=Yes\n', C);
fclose(fido);
end
function file = createfile(C)
file = sprintf('File "D:\\REF FAROS\\Faros\\S2k Model List\\Model%c.$2k was saved on m/d/yy at h:mm:ss"\n\n TABLE: "ACTIVE DEGREES OF FREEDOM"\n\n UX=Yes UY=No UZ=Yes RX=No RY=Yes RZ=No\n\n TABLE: "ANALYSIS OPTIONS"\n\n Solver=Advanced SolverProc=Auto Force32Bit=No StiffCase=None GeomMod=None HingeOpt="In Elements" NumAThreads=0 MaxFileSize=0 NumDThreads=0 NumRThreads=0 UseMMFiles="Program Determined" AllowDiff=No\n\n TABLE: "AUTO COMBINATION OPTION DATA 01 - GENERAL"\n\n DesignType=Steel AutoGen=No\n\n TABLE: "AUTO WAVE 3 - WAVE CHARACTERISTICS - GENERAL"\n\n WaveChar=Default WaveType="From Theory" KinFactor=1 SWaterDepth=45 WaveHeight=18 WavePeriod=12 WaveTheory=Linear\n\n TABLE: "CASE - MODAL 1 - GENERAL"\n\n Case=MODAL ModeType=Eigen MaxNumModes=12 MinNumModes=1 EigenShift=0 EigenCutoff=0 EigenTol=1E-09 AutoShift=Yes\n', C);
end
EDIT — (17 Mar 2024 at 05:35)
Added dir call to demonstrate that the files had been written.
.
Thanks, it works, but i dont understand the function writefile? What is C? What is wt? And how the interaction between writefile and createfile?
As always, my pleasure!
The ‘writefile’ function simply writes the file as you requested it. It needs to be a function because it has to open and close the file name it is writing to. The ‘C’ argument is the character that you want in the file name itself, and in the file name stored in the file that it refers to.
The ‘createfile’ function creates the contents of the file, however it does not create and save the files, as ‘writefile’ does. (I wrote ‘createfile’ to test the code. I forgot to delete it later, since it is no longer necessary. There is no interaction between them, because the essential code in ‘createfile’ is duplicated in ‘writefile’.)
The 'wt' argument to fopen tells fopen to create the file and to write text to it.
Hi star strider, can u help me again ?
if i arrange the string like this, can i do that ?
clear
clc
VariantModel = compose('%c', 'F':'G')
spacing = ' '
line1='File D:\REF FAROS\S2k File\TestModel%c.$2k was saved on m/d/yy at h:mm:ss'
line2='TABLE: "ACTIVE DEGREES OF FREEDOM"'
line3=' UX=Yes UY=No UZ=Yes RX=No RY=Yes RZ=No'
line4='TABLE: "ANALYSIS OPTIONS"'
line5=' Solver=Advanced SolverProc=Auto Force32Bit=No StiffCase=None GeomMod=None HingeOpt="In Elements" NumAThreads=0 MaxFileSize=0 NumDThreads=0 NumRThreads=0 UseMMFiles="Program Determined" AllowDiff=No'
combine = append(line1,spacing,line2,spacing,line3,spacing,line4,spacing,line5)
for k = 1:numel(VariantModel)
writefile(VariantModel{k})
end
files = dir('*.$2k');
files(:).name
function writefile(C)
fido = fopen("Model"+C+".$2k",'wt');
fprintf(fido,combine,C);
fclose(fido);
end
I doubt that will work.
All the ‘line1’ and such must be in the ‘writefile’ function, and they are not passed to it as arguments. The ‘writefile’ function will not pick up ‘combine’ from the workspace. (Also combine is actually a funciton name, so using it as a variable name is not advisable.)
It would be best to put all that inside ‘writefile’ from the outset, as I did in my code. I had it all as one fprintf call.
Making the appropriate changes and running the code —
VariantModel = compose('%c', 'F':'G')
VariantModel = 1x2 cell array
{'F'} {'G'}
for k = 1:numel(VariantModel)
writefile(VariantModel{k})
end
files = dir('*.$2k');
files(:).name
ans = 'ModelF.$2k'
ans = 'ModelG.$2k'
type(files(1).name) % See What The SAved Files Look Like
File D:\REF FAROS\S2k File\TestModelF.$2k was saved on m/d/yy at h:mm:ss TABLE: "ACTIVE DEGREES OF FREEDOM" UX=Yes UY=No UZ=Yes RX=No RY=Yes RZ=No TABLE: "ANALYSIS OPTIONS" Solver=Advanced SolverProc=Auto Force32Bit=No StiffCase=None GeomMod=None HingeOpt="In Elements" NumAThreads=0 MaxFileSize=0 NumDThreads=0 NumRThreads=0 UseMMFiles="Program Determined" AllowDiff=No
type(files(2).name) % See What The SAved Files Look Like
File D:\REF FAROS\S2k File\TestModelG.$2k was saved on m/d/yy at h:mm:ss TABLE: "ACTIVE DEGREES OF FREEDOM" UX=Yes UY=No UZ=Yes RX=No RY=Yes RZ=No TABLE: "ANALYSIS OPTIONS" Solver=Advanced SolverProc=Auto Force32Bit=No StiffCase=None GeomMod=None HingeOpt="In Elements" NumAThreads=0 MaxFileSize=0 NumDThreads=0 NumRThreads=0 UseMMFiles="Program Determined" AllowDiff=No
function writefile(C)
fido = fopen("Model"+C+".$2k",'wt');
spacing = ' ';
line1='File D:\\REF FAROS\\S2k File\\TestModel%c.$2k was saved on m/d/yy at h:mm:ss';
line2='TABLE: "ACTIVE DEGREES OF FREEDOM"';
line3=' UX=Yes UY=No UZ=Yes RX=No RY=Yes RZ=No';
line4='TABLE: "ANALYSIS OPTIONS"';
line5=' Solver=Advanced SolverProc=Auto Force32Bit=No StiffCase=None GeomMod=None HingeOpt="In Elements" NumAThreads=0 MaxFileSize=0 NumDThreads=0 NumRThreads=0 UseMMFiles="Program Determined" AllowDiff=No';
combine = append(line1,spacing,line2,spacing,line3,spacing,line4,spacing,line5);
fprintf(fido,combine,C);
fclose(fido);
end
Note that single backslant characters (\) are interpreted as control characters in fprintf and similar functions, so if you want to print a backslant character, it is necessary to ‘escape’ it by putting another backslant in front of it (\\).
You probably need to put some linefeed characters \n at the end of those lines (inside the last single quote) so that they do not all print on the same line. Other than that, it seems to work with those revisions. (Delete the type calls when you are happy with the file content they display.)
.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Produits

Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by