fprintf function
1 view (last 30 days)
Show older comments
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 Comments
Walter Roberson
on 3 Feb 2011
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.
Answers (1)
Rob Graessle
on 3 Feb 2011
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 Comments
Jan
on 3 Feb 2011
@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...
See Also
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!