How to create large (multiline) text file in matlab?
9 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(2 个)
David Young
2015-3-4
4 个评论
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.
Stephen23
2015-3-5
编辑:Stephen23
2015-3-5
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Environment and Settings 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!