How should I write this fprintf in order for it to work?

조회 수: 4 (최근 30일)
Ricardo Boza Villar
Ricardo Boza Villar 2016년 5월 6일
댓글: Ricardo Boza Villar 2016년 5월 6일
I have this:
lambda=2;
fprintf(['%%%%%%%%%%%%%%%'...
' lambda=%d '...
'%%%%%%%%%%%%%%%\n'],lambda)
fprintf('%%%%%%%%%%lambda=%d %%%%%%%%%%%%%%%%%\n',lambda)
Neither of them seem to work. If I write them separately, the first one doesn't give the answer because it cuts it. The second gives the answer but doesn't display all the characters '%' I have written at the end. Plus, if they are together, only the second one works (partly, as I've described).
What should I do in order for them to work (together and separately)?
PS: I want to use the first fprintf in this program:
clear, format compact, format shortg
myoptions=optimoptions('fsolve','tolx',1e-12,'maxiter',100,'display','off');
for lambda=0:2
fprintf(['%%%%%%%%%%%%%%%'...
' lambda=%d '...
'%%%%%%%%%%%%%%%\n'],lambda)
[x,fval,exitflag]=fsolve(@(x)a57fun(x,lambda),rand(3,1),myoptions)
fprintf(['El valor de x(2) con 8 cifras significativas'...
'para lambda=%d es %1.8f \n'],lambda,x(2))
end
function [F]=a57fun(x,lambda)
A=[6 -1 2; -1 4 3; 2 3 5];
ter1=exp(2*x(1)^2+x(2)^2-1);
ter2=sinh(5*x(2)-x(3));
g=[4*x(1)*ter1; 2*x(2)*ter1+5*ter2; -ter2];
b=[1;2;3];
F=A*x+lambda*g-b;
end

채택된 답변

Stephen23
Stephen23 2016년 5월 6일
편집: Stephen23 2016년 5월 6일
Beginners often complain that some function "does not work" properly, and very often they did not bother to read the documentation. When you read the fprintf documentation then you will learn that '%' is a special character that needs special handling (called escaping).
Hint: the documentation tells us how MATLAB works: use it.
Both of these examples show print ten % characters in a row, and none of them disappear!
Solution One: Escape the Format String Properly
lambda=2;
fmt = repmat('%%',1,10);
fmt = sprintf('%s lambda = %%d %s\\n',fmt,fmt);
fprintf(fmt,lambda)
prints this:
%%%%%%%%%%lambda = 2 %%%%%%%%%%
Solution Two: use Input Arguments Properly
lambda=2;
fmt = repmat('%',1,10);
fprintf('%s lambda = %d %s\n',fmt,lambda,fmt)
prints
%%%%%%%%%%lambda = 2 %%%%%%%%%%
  댓글 수: 1
Ricardo Boza Villar
Ricardo Boza Villar 2016년 5월 6일
You suppose too much.
  1. I'm not saying 'This MATLAB function doesn't work', I'm saying 'How can I write this function in order for it to work?'
  2. I had read the documentation on 'fprintf' before I asked this question.
  3. I don't consider myself a beginner. If I'm an expert at something I don't tell other people what I think they are.
Thank you for your answer, though. Even if it has its flaws, it's quite complete.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2016년 5월 6일
편집: Azzi Abdelmalek 2016년 5월 6일
lambda=2;
fprintf('%%%%%%%%%% lambda=%d %%%%%%%%%%\n',lambda)
The number of % should be even. Because to display the special character %, you need to represent it with %%

카테고리

Help CenterFile Exchange에서 Data Type Identification에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by