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

30 次查看(过去 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