필터 지우기
필터 지우기

How do you get a number string to display the sign (pos and/or neg) in front of it.

조회 수: 24 (최근 30일)
Sky Adams`
Sky Adams` 2015년 4월 13일
댓글: Sky Adams` 2015년 4월 13일
What i am having trouble finding is how to get a title of a graph to display the sign for both positive and negative input. What i have done so far is this.
%Determine what the quadratic is.
disp('What are the quadratics coefficients?');
a = input('Coefficient A=');
b = input('Coefficient B=');
c = input('Variable C=');
disp('Thank you, calculating now.');
%display graph and customize it
plot(x,y, '--b', 'LineWidth', 3)
grid on
xlabel ('x')
ylabel ('f(x)')
title (['f(x)=' num2str(+a) 'x^2' num2str(+b) 'x' num2str(+c)])
The title is the quadratic function but like this only shows the "-" but i need it to show the "+" if it is a positive number.
Any pointers?

답변 (2개)

Guillaume
Guillaume 2015년 4월 13일
Use a format specifier with your num2str:
>>num2str(1.5235646, '%+2.2f')
ans =
+1.52
I would actually use sprintf to build the whole string:
title(sprintf('f(x)= %+2.2fx^2%+2.2fx%+2.2f', a, b, c))
  댓글 수: 1
Sky Adams`
Sky Adams` 2015년 4월 13일
Great, thanks for the fast reply! And thanks for the pointers to the right direction. :)

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


pfb
pfb 2015년 4월 13일
Perhaps use sprintf? For instance, if a, b, c are integers
ttl = sprintf('f(x) = %+d x^2 %+d x %+d',a,b,c);
title(ttl);
Use %f if a,b,c, are floating point and so on. Refer to the help of sprintf.
  댓글 수: 2
pfb
pfb 2015년 4월 13일
sorry for the duplicate, didn't see Guillaume's reply.
Sky Adams`
Sky Adams` 2015년 4월 13일
No need to apologize. I found your answer a bit more aligned with what i know which in turn made Guillaume's easier to understand. Thank you :)

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

카테고리

Help CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by