How to create large (multiline) text file in matlab?
9 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hello
I need to have a part of the matlab program which will create large text file, which will be source code for another program. FYI, the result text file should look like following:
-----------------------------------------------------------------------------------------------------
_.Units um .freq fmin=2.500000e+009 fmax=10e+09 ndec=1 ******************************************************************************
.Default h=1.000000e-001 lambda=7.117625e-007 sigma=0.000000e+000
****************************************************************************
.Default z=0 nwinc=10 nhinc=5
*Coil Nodes
*CENTRAL WIRE COORDINATES
NA1 x=0 y=-2000
NA2 x=0 y=0_
-----------------------------------------------------------------------------------------------------
Is it possible to create such a large string and write it in to file, lets say myprogram.txt.
Please help. Thanks a lot in advance
0 comentarios
Respuestas (2)
David Young
el 4 de Mzo. de 2015
4 comentarios
David Young
el 5 de Mzo. de 2015
What Adam said is what most programmers would do. To give more detailed advice, I'd need to know how the data you want to write is represented in MATLAB.
Stephen23
el 5 de Mzo. de 2015
Editada: Stephen23
el 5 de Mzo. de 2015
Use MATLAB's tools to help you. Try this:
- save the template text in a file.
- replace every variable instance with the appropriate format specifier.
- read this file into MATLAB using fileread, as a string
- Use this string as the format string in an fprintf call, with the appropriate variables.
Here is how this template file would be used, as pseudocode:
for k = 1:numFiles
str = fileread(templateFileName);
fid = fopen(newFileName,'wt');
fprintf(fid,str,var1,var2,var3...)
fclose(fid)
...
... call other code and external program here
end
Note that your text file has thirteen numeric variables: this is a lots to enter as individual inputs to fprintf, but it is also possible to enter them via a cell array like this:
vec = {var1,var2,var3,...};
fprintf(fid,str,vec{:})
This can be a bit neater in some situations.
I have uploaded your example textfile with format specifiers replacing every numeric value. You can read the fprintf documentation to select the best format specifiers for your data.
0 comentarios
Ver también
Categorías
Más información sobre Environment and Settings en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!