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

1 回表示 (過去 30 日間)
saharsahar
saharsahar 2012 年 4 月 20 日
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

採用された回答

Hossein
Hossein 2012 年 4 月 20 日
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.

その他の回答 (1 件)

Srinivas
Srinivas 2012 年 4 月 20 日
help xlswrite
help fprintf
may be you can do quick google search for this

カテゴリ

Help Center および File ExchangeSpreadsheets についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by