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

조회 수: 12 (최근 30일)
Cloud
Cloud 2015년 3월 4일
편집: Stephen23 2015년 3월 5일
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

답변 (2개)

David Young
David Young 2015년 3월 4일
Yes, see
doc fprintf
or look here. Multiple lines are achieved using the \n format specifier.
  댓글 수: 4
David Young
David Young 2015년 3월 5일
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 2015년 3월 5일
편집: Cloud 2015년 3월 5일
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.

댓글을 달려면 로그인하십시오.


Stephen23
Stephen23 2015년 3월 5일
편집: Stephen23 2015년 3월 5일
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.

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by