Borrar filtros
Borrar filtros

fprintf function

5 visualizaciones (últimos 30 días)
Jason
Jason el 3 de Feb. de 2011
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 comentarios
Walter Roberson
Walter Roberson el 3 de Feb. de 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.
Jason
Jason el 3 de Feb. de 2011
I'm using the txt file just an example here. I do not have any problem opening whatever object I have. But yes this is more like Object oriented programming. I can write to the object with this code:
fprintf(object, 'The frequency is 2 GHz');

Iniciar sesión para comentar.

Respuestas (1)

Rob Graessle
Rob Graessle el 3 de Feb. de 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 comentarios
Jason
Jason el 3 de Feb. de 2011
I don't think it has anything to do with how I open the file. For Example, this code work but not the one I had:
fprintf(object, 'The frequency is 2 GHZ');
Jan
Jan el 3 de Feb. de 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...

Iniciar sesión para comentar.

Categorías

Más información sobre Startup and Shutdown en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by