필터 지우기
필터 지우기

call different functions

조회 수: 3 (최근 30일)
hajer
hajer 2011년 12월 2일
Hi i have alot of functions (same idea and different work), and i will print to user to select any function that he want .then in my code i want to call the function that he chose. Can I do that? note: the name of function was stored in variable . for example : functions ( fun1(x,y) , fun2(x2,y2) ) enter tne name of function : fun1 How i can call func1 in code ????????

채택된 답변

Chandra Kurniawan
Chandra Kurniawan 2011년 12월 2일
Hello,
I have 'fun1.m'
function fun1(x,y)
disp(x+y);
I have 'fun2.m'
function fun2(x2,y2)
disp(x2-y2);
Then I have a m-file
clear; clc;
funList = {'fun1','fun2'};
disp('1] fun1(x,y) 2] fun2(x2,y2)');
choice = input('Select function [1/2] : ');
funstr = str2mat(funList(choice));
hnd = str2func(funstr);
var1 = input('Input 1st parameter : ');
var2 = input('Input 2nd parameter : ');
hnd(var1,var2);
Just try to run it.
  댓글 수: 1
hajer
hajer 2011년 12월 2일
thank you very much

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

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by