MATLAB: Sprintf/fprintf help


n=10 if i want the output to say "You have done this activity 10 times" how would i write it? I know for fprintf('You have done this activity \n'), the n would go in there, but is there a write is so that i could include words after \n? And should i use sprintf or fprintf?

Best Answer

  • Try this:
    N = 10;
    str = sprintf('You have done this activity %d times.\n', N);
    fprintf(fido, 'You have done this activity %d times.\n', N)
    The fprintf function prints its output to the Command Window or to a file (identified by ‘fido’ here, and created by the fopen function).
    The sprintf function outputs to a string (here the ‘str’ variable) that you can use for other purposes, for example a text object, plot title, xlabel, ylabel, zlabel and other situations where you want formatted output.
    See the documentation for the various functions for a full description of their capabilities.