Sprintf warning message on a function file

Hello, i created a function file where need to use sprintf, but theres a warning message that say to use fprintf instead, in this case i really need to use sprintf, because i´m exctracting specific values from a vector, and fprintf wil print a number before the text that i printed... do you know any alternative that works like sprintf, to make the warning message go away? Thanks a lot!
%On a function file...
%rest of my code...
equacao_1=sprintf('The equation of the fitted function is: y= %f*x + (%f).',p1(1),p1(2));
disp(equation_1),disp(' ')
This is the message that appears...the program run, but i don´t want to see this message.

 采纳的回答

The warning is saying that, if all you do with the result from sprintf is to display it with disp, then you can display it with fprintf instead.
p1 = [5 6];
equacao_1=sprintf('The equation of the fitted function is: y= %f*x + (%f).',p1(1),p1(2));
disp(equacao_1)
The equation of the fitted function is: y= 5.000000*x + (6.000000).
fprintf('The equation of the fitted function is: y= %f*x + (%f).\n',p1(1),p1(2));
The equation of the fitted function is: y= 5.000000*x + (6.000000).
If you use equacao_1, the result from sprintf, somewhere else in your code to do something besides display it with disp, for example to set it to the title of a plot, then the warning will go away. Alternatively, you can right-click and disable the warning (on that line, in that file, or in all files), or set the warning to be disabled by editing your preferences (Preferences>Code Analyzer).

5 个评论

Thank u so much for your answer!!
I´m trying to disabled by editing your preferences (Preferences>Code Analyzer), like u said, appears this:
Where do I click for disable that? It´s appear like a 'Info' on Code Analyzer.
I forgot to metion that p1 came from a polyfit vector
( p1=polyfit(Z(:,1),Z(:,2),1);), i think that´s why when i use fprint appears this number before the text for example:
equacao_1=fprintf('The equation of the fitted function is: y= %f*x + (%f).',p1(1),p1(2));
disp(equacao_1)
%What appears on comand window its this, and when i use sprintf, works just
%fine:
%The equation of the fitted function is: y= 1.010720e+00*x + (1.093415e+00). 67
%With sprintf:
%The equation of the fitted function is: y= 1.010720e+00*x + (1.093415e+00).
(The Code Analyzer>Preferences window I was talking about is accessed from the main MATLAB window. Be sure you are following the instructions in the link I gave.)
Perhaps better than disabling this warning is to figure out why there is the extra number when you use fprintf. Notice that you are storing the return value from fprintf and displaying that. That's appropriate for sprintf but not for fprintf. fprintf returns the number of bytes printed; that's why you see "67".
p1 = [5 6];
Compare what you have:
equacao_1=fprintf('The equation of the fitted function is: y= %f*x + (%f).',p1(1),p1(2));
The equation of the fitted function is: y= 5.000000*x + (6.000000).
disp(equacao_1)
67
with what I have (not storing the result of fprintf and not using disp):
fprintf('The equation of the fitted function is: y= %f*x + (%f).\n',p1(1),p1(2));
The equation of the fitted function is: y= 5.000000*x + (6.000000).
Notice that the warning is clear about this. It does not say that sprintf(...) can be replaced by fprintf(...). It says that disp(sprintf(...)) can be replaced by fprintf(...\n).
In other words, it says you can remove disp, change sprintf to fprintf, and add a newline (\n) to the character vector you are printing.
Oh wow Voss thanks a lot!! It´s working just fine, like u said in ur explanation!
Ur solution:
%with what I have (not storing the result of fprintf and not using disp):
%fprintf('The equation of the fitted function is: y= %f*x + (%f).\n',p1(1),p1(2));
%The equation of the fitted function is: y= 5.000000*x + (6.000000).
Thank u so so much!!!

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Annotations 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by