MATLAB: Writing parameters to a text file

text file

Hello all,
I was hoping that someone could help me with writing a text file in matlab. What i have is a program that has many initial parameters and then goes through stages of calculations and graphing. at the end of the program i use the imwrite command to save the pictures however, what i am wondering is how to save a text file with a list of the initial parameters and what ever other information i want…
something of the form:
LOG INFORMATION
Wavelength:
Source to Object:
etc.
Thank you BN

Best Answer

Writing a text file in MATLAB is very easy. Take a look at the documentation for the function fopen, fprintf, and fclose.
doc fopen
doc fprintf
doc fclose
On a quick thought, a brief example:
fileID = fopen('myFileName', 'wt'); % Open and create file in text writing mode.
% Write the content to the log text file.
fprintf(fileID, ' =======================================\r\n');
fprintf(fileID, '|| LOG INFORMATION ||\r\n');
fprintf(fileID, ' =======================================\r\n\r\n\r\n');
fprintf(fileID, 'Wavelength\t%s\r\n', wavelength); % "wavelength" is a string.
fprintf(fileID, 'Source Object\t%s\r\n', sObject); % "sObject" is a string.
fprintf(fileID, 'Value\t%f\r\n', value); % "value" is a float.
fclose(fileID); % Close file.
Notice that \r\n is necessary for a new line in Windows (more information in fprintf documentation).