How to create large (multiline) text file in matlab?
8 views (last 30 days)
Show older comments
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 Comments
Answers (2)
David Young
on 4 Mar 2015
4 Comments
David Young
on 5 Mar 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
on 5 Mar 2015
Edited: Stephen23
on 5 Mar 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 Comments
See Also
Categories
Find more on Environment and Settings in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!