-1

function parameter includes h(=10). I want plot title to include the same h. how to do?

function G=graphit(X,Y,ye,h)
plot(X,Y,'-'); 
grid
title([ 'Approximate and Exact Solution @h= .', num2str(h)])

Thanks. MM

Mary A. Marion
  • 780
  • 1
  • 8
  • 27

4 Answers4

0
title(['Approximate and Exact Solution ',num2str(h),' .'])
Adam
  • 414
  • 2
  • 8
0

You can use sprintf to create a formatted string

title( sprintf( 'Approximate and Exact Solution. h = %.0f', h ) );
Wolfie
  • 27,562
  • 7
  • 28
  • 55
0
title_string = sprintf('Approximate and Exact Solution @h= %d.',h) % change d to f for floats
title(title_string)

I'd use a proper string-formatting tool such as sprintf for building correctly formatted titles.

Adriaan
  • 17,741
  • 7
  • 42
  • 75
0

Despite 3 excellent answers given in less than 5 minutes, none of the suggested code would run properly. Basically, I got almost identical results as when I ran my original code.

It turns out that leading zeroes in a number for h such as 01 or 05 will cause the system to drop the zero. This was a problem for me since I wanted h values to be as they are .05, .025, .01. Further, the Matlab software seemed to become confused with a specified decimal point followed by a number with leading zeroes. The way around this was to pass the decimal point with the h value (.10,.05,.025,.01). See code below.

Input is

X,Y,xe,ye,.01

Working code:

function G=graphit(X,Y,xe,ye,h)
hold on; 
plot(X,Y,'-'); plot(X,ye,'-.'); 
hold off
title([ 'Approximate and Exact Solution @h=', num2str(h)])

Expected and attained output: Approximate and Exact Solution @h=0.01

Voila! Thanks for those replies...

Mary A. Marion
  • 780
  • 1
  • 8
  • 27
  • 2
    So you wanted to print "0.01" but was passing the value 10 instead? That makes no sense... – Cris Luengo Mar 13 '19 at 03:30
  • This shows why it's so important to include a [mcve] in your question - this answer does not describe the same problem stated in the question. – Wolfie Mar 13 '19 at 07:19
  • I was trying to save keystrokes when I entered h i.e 10 rather than .10. I will include minimal complete and verifiable example next time. Thank you for the suggestion. I have been trying to do minimal complete. – Mary A. Marion Mar 14 '19 at 11:13