How to create large (multiline) text file in matlab?

9 visualizaciones (últimos 30 días)
Cloud
Cloud el 4 de Mzo. de 2015
Editada: Stephen23 el 5 de Mzo. de 2015
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

Respuestas (2)

David Young
David Young el 4 de Mzo. de 2015
Yes, see
doc fprintf
or look here. Multiple lines are achieved using the \n format specifier.
  4 comentarios
David Young
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.
Cloud
Cloud el 5 de Mzo. de 2015
Editada: Cloud el 5 de Mzo. de 2015
It is little bit more complicated. My program works like this:
1. Calculate some variables, which have to be included in the text file
2. Put the calculated values in proper position in the text file
3. Text file is an another source code for a simulation program, so matlab runs the external program with created text file
4. Read the output of the external (fortran based) program an exclude the results
5. Work with the simulated values
I have done everything except step 2. The source code fort the simulation program is much longer than the shown sample, so it seems to be very obsolete write every single line by using the fprintf command.
Is there some other way, how to do it more comfortably? Thanks a lot.

Iniciar sesión para comentar.


Stephen23
Stephen23 el 5 de Mzo. de 2015
Editada: Stephen23 el 5 de Mzo. de 2015
Use MATLAB's tools to help you. Try this:
  1. save the template text in a file.
  2. replace every variable instance with the appropriate format specifier.
  3. read this file into MATLAB using fileread, as a string
  4. 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.

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!

Translated by