How to exported formatted text and create files in array with specified variables ?
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Arif
el 17 de Mzo. de 2024
Comentada: Star Strider
el 22 de Mzo. de 2024
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1644121/image.png)
0 comentarios
Respuesta aceptada
Star Strider
el 17 de Mzo. de 2024
Editada: Star Strider
el 17 de Mzo. de 2024
There are several ways to do this —
VariantModel = compose('%c', 'A':'E')
files = sprintf('''Model%c.$2k'' (or it may be .txt extension) (contains text from line 6 to line 22)\n',VariantModel{:})
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{:}
Then for each file:
filename = files{1}
And so for the others.
EDIT — Added ‘for each file’ example at the end.
.
6 comentarios
Star Strider
el 22 de Mzo. de 2024
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')
for k = 1:numel(VariantModel)
writefile(VariantModel{k})
end
files = dir('*.$2k');
files(:).name
type(files(1).name) % See What The SAved Files Look Like
type(files(2).name) % See What The SAved Files Look Like
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.)
.
Más respuestas (0)
Ver también
Categorías
Más información sobre Formalize Requirements in Simulink Models en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!