fprintf function
1 回表示 (過去 30 日間)
古いコメントを表示
I am trying to do a fprintf() to an object, here is my code:
object = fopen('file.txt');
string1 = '2';
string2 = 'GHz';
fprintf(object, 'The frequency is %s %s', string1, string2);
I have tried many variation of this but it keeps giving an error of too many inputs. String1 and string2 will change accordingly, that is why I have it like that. Please help me get this to output the right string. Thanks.
2 件のコメント
Walter Roberson
2011 年 2 月 3 日
Is there are a reason you specifically say that you are trying to fprintf to an _object_ rather than saying that you are fprintf to a text file ? Do you mean to imply an object as in Object oriented programming? If your "object" is not the standard fopen(), perhaps an overridden method, then the fprintf() for that object class needs to be defined with varargin.
回答 (1 件)
Rob Graessle
2011 年 2 月 3 日
Try opening your text file like this:
object = fopen('file.txt', 'w');
This will specify that the file object is to be used for writing to the file - see the 'permission' argument in the function documentation. The rest of your syntax looks good. Don't forget to close the file object when you're done writing to it:
fclose(object);
3 件のコメント
Jan
2011 年 2 月 3 日
@Walter: 'w' works absolutely correct with text files. The 't' in the text mode influences the internal conversion of CHAR(10) to CHAR([13, 10]) and interpretation of CHAR(26) as "end of file". But even under Windows you can write a valid text file with FOPEN(File, 'w'). I'm sure, beginners are unnecessarily confused by the silent internal text mode conversions - and Linux simply does not care at all...
参考
カテゴリ
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!