Help using fprintf function

7 views (last 30 days)
Raymond Elliott
Raymond Elliott on 10 Oct 2019
Commented: DGM on 15 Jun 2025
I am writing a .m file to later call in my main script that will use the bisection method to solve a problem. However, on my fprintf line I am getting the error, "The format might not agree with the argument count." I am new to matlab and am unsure what I am doing wrong. Any help is greatly appreciated.
function [xr,fr,ea,iter] = bisection(f,xl,xu,es,imax)
if nargin < 3,disp('at least 3 input arguments required'),end
fl = f(xl); fu = f(xu); test = fl*fu;
if test > 0,disp('@@ Bad initial guess, no sign change'),end
if nargin < 4, es = 0.0001;end
if nargin < 5, imax = 50;end
xr = xl;
disp('Iter xl xu xr ea es')
for iter=1:1:imax
xrold = xr;
xr = (xl + xu)/2; fr = f(xr);
if xr ~= 0,ea = abs((xr - xrold)/xr) * 100;end
fprintf('%4d\t%6.4f\t%6.4f\t%6.4f\t%8.6f\t%8.6f\n’,iter,xl,xu,xr,ea,es')
test = fl*fr;
if test < 0
xu = xr;
elseif test > 0
xl = xr; fl = fr;
else
ea = 0;
end
if ea <= es,break,end
end
end
  2 Comments
Walter Roberson
Walter Roberson on 11 Oct 2019
Why do you use es' for fprintf when you expect es to be a scalar ?
Note that you continue executing in the situation tat nargin < 3
if nargin < 3,disp('at least 3 input arguments required'),end
You should probably error() instead of disp()
dpb
dpb on 11 Oct 2019
Edited: dpb on 11 Oct 2019
Irrespective of the Q?,
function [xr,fr,ea,iter] = bisection(f,xl,xu,es,imax)
if nargin < 3,disp('at least 3 input arguments required'),end
is admirable to do error checking, but your function as is just goes on its merry way after the test, anyways...
function [xr,fr,ea,iter] = bisection(f,xl,xu,es,imax)
if nargin < 3,error('At least 3 input arguments required'),end
will display the message and then abort the function. You can get more involved as you wish, but that's the simplest possible...
But, as you've written it in the function definition line, the function will abort unless all five arguments are supplied...see the doc for varargin to see how to handle a variable number of arguments.
Walter commented while I was still editing before I got to the fprintf() conundrum; not possible to tell unequivocally w/o seeing the input argument definitions and the calling sequence, but the use of the transpose is peculiar and may be what's triggering the error.

Sign in to comment.

Accepted Answer

Voss
Voss on 26 Dec 2023
fprintf('%4d\t%6.4f\t%6.4f\t%6.4f\t%8.6f\t%8.6f\n',iter,xl,xu,xr,ea,es)
  1 Comment
DGM
DGM on 15 Jun 2025
It's plausible that the inadvertent use of a curly apostrophes was the root of the whole problem. How does that happen? Do people often accidentally type lone curly closing apostrophes? I suppose it's possible, but my guess is that it starts like this.
You copy and paste a pretty-formatted example from a website:
fprintf(%d/n’,x) % a pretty-formatted example
Then you adapt it for your usage:
fprintf(%4d\t%6.4f\t%6.4f\t%6.4f\t%8.6f\t%8.6f\n’,iter,xl,xu,xr,ea,es)
This will produce a specific error pointing to the first bad apostrophe:
Invalid text character. Check for unsupported symbol, invisible character, or pasting of non-ASCII characters.
So the user follows the instructions and tries to troubleshoot by retyping the apostrophe. Depending on the text scaling and font, it might not even be apparent that the replacement character is different, but the linter highlighting does immediately change:
fprintf('%4d\t%6.4f\t%6.4f\t%6.4f\t%8.6f\t%8.6f\n’,iter,xl,xu,xr,ea,es)
Upon changing the character, the error becomes
Character vector is not terminated properly
Given that the entire argument list is underlined in a squiggle, the unfamiliar user may simply follow guidance and terminate the argument list with another apostrophe, leading to the apparent transpose of es that Walter mentioned. The highlighting has changed, so we must've done something right:
fprintf('%4d\t%6.4f\t%6.4f\t%6.4f\t%8.6f\t%8.6f\n’,iter,xl,xu,xr,ea,es')
Now we've recreated the problem as given.

Sign in to comment.

More Answers (1)

KURRA HARIKRISHNA
KURRA HARIKRISHNA on 14 Jun 2025
Edited: Walter Roberson on 14 Jun 2025
fprintf('%4d'. i*j);
  1 Comment
Walter Roberson
Walter Roberson on 14 Jun 2025
Period by itself is not a valid binary operator that can go inbetween '%4d' and i*j . You probably wanted comma there,
fprintf('%4d', i*j)
Note that the effect of that code fragment depends on whether it is in LiveScript or not, and whether it is part of a longer line in LiveScript.
If the fragment is executed in LiveScript, then all of the fprintf() on the same (potentially continued) input line output together, and after the last of them there is an implicit newline emitted (unless the last fprintf already ended its output with newline.)
If the fragment is executed outside of LiveScript, then the output will not have an implicit newline added.
Note that there is also no implied space added between outputs. So for example
i = 3; for j = [400,500]; fprintf('%4d', i*j); end
will output
12001500
with no space between them.
Because of this, the fprintf() statement would typically be either
fprintf('%4d\n', i*j)
or else
fprintf(' %4d', i*j)

Sign in to comment.

Categories

Find more on Data Type Identification in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!