add two number and create the text file with the result in it

2 views (last 30 days)
Hi All, I want to create a very small program which gives the result in an excel sheet or text file. for example it does (2+3) and create a text file in a directory and gives the result (5) in the text file. My MATLAB version is 2011.
Thanks

Accepted Answer

Hossein
Hossein on 20 Apr 2012
Hi, first create a file using fopen function. It opens/creates to read/write. Because you wanna write use 'w' as permission. fopen returns the file ID.
fileID=fopen('c:\blahblah\mytext.txt','w');
then use fprintf function to write txt into your file. If you don't specify file ID, matlab will display the result in the command window.
fprintf(fileID,formatdata,data);
you need to format your data before sending it to txt file. Let say data is data=[1, 2.3333]; then you do the following:
fprintf(fileID,'%d %f',data);
%d is used for integers and %f for the float type data. for more details get help on fprintf, fopen.
There might be easier way when you have a large data file. But I can't think of anything except writing a loop and changing the cursor position in file.

More Answers (1)

Srinivas
Srinivas on 20 Apr 2012
help xlswrite
help fprintf
may be you can do quick google search for this

Community Treasure Hunt

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

Start Hunting!