Is there a way I can get my code to display the function chosen in fprintf?

1 vue (au cours des 30 derniers jours)
JJ
JJ le 21 Fév 2018
Modifié(e) : Stephen23 le 21 Fév 2018
Is there a way to tell my code to put my chosen function into and fprintf?
i have a function: f=@ x.^2;
fprintf('find the root of \n, ??) I tried putting f into the question marks but it says Error using fprintf Function is not defined for 'function_handle' inputs.
i know i can just leave the semi colon off to display it, but wanted it to look a bit nicer with words

Réponse acceptée

Stephen23
Stephen23 le 21 Fév 2018
Modifié(e) : Stephen23 le 21 Fév 2018
It is not necessary to jump into using the symbolic toolbox for every little problem: that would be overkill for such a simple problem like this. This problem is easily solved using func2str:
>> f = @(x) x.^2;
>> fprintf('find the root of %s\n', func2str(f))
find the root of @(x) x .^ 2
If you want to remove the leading @(...) then use regexprep:
>> str = regexprep(func2str(f),'^@\([^\)]\)\s*','');
>> fprintf('find the root of %s\n', str)
find the root of x .^ 2

Plus de réponses (0)

Catégories

En savoir plus sur Entering Commands dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by