Is there a way to suppress output, given a condition, from within fprintf?

조회 수: 19 (최근 30일)
Further to this question, is there a way to suppress output of the imaginary part if it is 0 (i.e. there is no imaginary part) from within fprintf?
I used the linked question's answers to write this code for printing quadratic roots:
fprintf("Primera raíz: %.4f + %.4fi\n",real(first),imag(first));
fprintf("Segunda raíz: %.4f + %.4fi\n",real(second),imag(second));
Resulting in output like:
Primera raíz: -0.3596 + 0.0000i
Segunda raíz: -1.3904 + 0.0000i

채택된 답변

Image Analyst
Image Analyst 2021년 3월 14일
What about using an if statement
if imag(first) == 0
fprintf("Primera raíz: %.4f \n", first);
else
fprintf("Primera raíz: %.4f + %.4fi\n", real(first), imag(first));
end
if imag(second) == 0
fprintf("Primera raíz: %.4f \n", second);
else
fprintf("Segunda raíz: %.4f + %.4fi\n", real(second), imag(second));
end
  댓글 수: 1
Monica W
Monica W 2021년 3월 15일
편집: Monica W 2021년 3월 15일
Thanks for the suggestion!
I figured I could use an if statement, I was just curious whether it was possible to somehow get shorter code doing it from the fprintf function (even though, reading the documentation, it doesn't really look like it - but sometimes there are other functions I'm not yet aware of that can do what I'm thinking, or ways of using a function that are not entirely clear from the documentation, so I thought I'd ask). :)

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

추가 답변 (1개)

John D'Errico
John D'Errico 2021년 3월 15일
편집: John D'Errico 2021년 3월 15일
Could you do it? Well, probably. You would need to construct some pretty elaborate conditional operations inside the fprintf.
For example, you would need to modify the first argument to fprintf depending on if there was an imaginary part found. I can think of several complicated ways to do that. In the end, any code I can think of to do this ends up being far more difficult to read and debug than a trivially easy to read and write if statement!
NEVER write complicated code to do something trivial. Your code will suffer greatly, in becoming literally impossible to read and debug in the future. Unless there is a significant gain in speed or memory required, avoid the complicated code.
Shorter code is not a goal. You don't pay by the character used to write that code. MATLAB does not execute code with 2 fewer characters in it more quickly.
So think of yourself, one day, inheriting some nasty, complex rats nest of spaghetti code when your co-worker gets run down by the crosstown buss. Suddenly you are told to maintain a code base that you cannot even read. How will you feel then? So no. Please, do yourself and your co-workers a great boon. Write clean, simple code that is easy to read and maintain.
  댓글 수: 2
John D'Errico
John D'Errico 2021년 3월 15일
Yes, I know that 5 or 10 years from now, someone will flag this answer as a rant. But that would miss the important point - that as you learn to code, if you learn nothing else, it is that you would never write code with the sole purpose of writing code. So your goal as a programmer is to write simple code that does the job simply, that will not force you to spend time thinking it out and then maintaining it in the future. Never look for elegance for the purpose of elegance. Get the job done, so that you can start the next job.
Monica W
Monica W 2021년 3월 27일
Hey John, I get what you're saying. This was definitely more of a 'what if' question rather than a 'should I'? I definitely don't want to become a cause of anguish in my future colleagues! Point taken. :)

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

카테고리

Help CenterFile Exchange에서 Entering Commands에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by