How to write a function that generates a hyperlink that runs a function?

조회 수: 2 (최근 30일)
I'm looking for a way to run a random function with input arguments by clicking a hyperlink in the command window.
In my case, I wrote some functions (Riemann method, Trapezoidal Rule, etc.) to approximate the value of definite integrals. They all have f, a, b and n as input arguments, where f is a function implemented with the syms operator (e.g. f(x)=x^2), a and b are the bounders and n is the amount of intervals. I would now like to 'collect' all these functions in one new function, named integral with the same input arguments, that displays a hyperlink in the command window for each 'integrate function'. So, when one clicks such a hyperlink, the corresponding function is runned. I tried:
function [ ] = integral( f,a,b,n )
disp('<matlab:riemann(f,a,b,n) Riemann>')
disp('<matlab:trapezoid(f,a,b,n) Trapezoid>')
...
end
This doesn't work however, because the f, a, b and n in the display apparently aren't defined.
Is there a way to achieve my goal? Alternatives like pop-up windows and buttons instead of hyperlinks are welcome aswell.
Thanks in advance!

채택된 답변

Walter Roberson
Walter Roberson 2013년 3월 30일
fprintf('<matlab:plus(%d,%d) Sum>\n', a, b)
Note change from sum() to plus() -- unless, that is, you want "b" to be the dimension over which to sum the array "a"
  댓글 수: 3
Walter Roberson
Walter Roberson 2013년 3월 30일
편집: Walter Roberson 2013년 3월 30일
For your expanded question:
function [ ] = Link_integral( f,a,b,n )
fstr = ['@(x) ' char(f)];
intargs = sprintf('(%s,%g,%g,%d)', fstr, a, b, n);
fprintf('<matlab:riemann%s Riemann>\n', intargs);
fprintf('<matlab:trapezoid%s Trapezoid>\n', intargs);
end
The function would be slightly different if you want the symbolic form to continue to be received as symbolic:
fstr = ['sym(''' char(f) ''')'];
Jeroen
Jeroen 2013년 3월 30일
편집: Jeroen 2013년 3월 30일
I get 2 errors:
Error using sym/subsindex (line 1367)
Indexing input must be numeric, logical or ':'.
Error in riemann (line 4)
f(x)=f;
Edit: O, I've been a bit too fast! It works when I use
fstr = ['sym(''' char(f) ''')'];
Thanks a lot for helping me Walter!

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

추가 답변 (0개)

제품

Community Treasure Hunt

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

Start Hunting!

Translated by