input function as plot title?

조회 수: 3 (최근 30일)
Emmalie Foti
Emmalie Foti 2020년 4월 23일
답변: Johannes Hougaard 2020년 4월 23일
im creating a function that plots a user input function. i want the title to be just the function entered, but when i run the code, it displays the whole long vector that the function outputs.
fh=input('Enter a function: ');
minimum=min(fh)
maximum=max(fh)
plot(x,fh)
axis([a b floor(minimum) ceil(maximum)])
title(sprintf('%s',fh))
for example, if i input x^2 as the function, the graph would use 0 4 9 16 etc. as the title rather than just x^2.

채택된 답변

madhan ravi
madhan ravi 2020년 4월 23일

추가 답변 (1개)

Johannes Hougaard
Johannes Hougaard 2020년 4월 23일
I think this would do it
function_name = input('Enter a function:','s');
fh = str2func(strcat('@(x)',function_name));
fplot(fh,[min(x) max(x)]);
title(sprintf('%s',strrep(function_name,'.','')))

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by