Opening a file and loading a function from another script

조회 수: 3 (최근 30일)
Dan
Dan 2011년 12월 16일
Hi,
I want to run a function from a .m file that is chosen by a user via something like uigetfile() while running another script. The function that will be called is the only function in the opened .m file and shares a common title with the .m file, but is in a different directory than the currently running script and takes in two input parameters. Someone helped me run a script from another script using run(fullfile(directory, fileName)), but I'm not sure if it'll work here. I also tried to use uigetfile to load the .m file containing the function and then call the function, but that didn't work either.
I'm not sure if I made myself totally clear with what I need. My MATLAB terminology isn't very good, but I'll be happy to clear things up for you as much as I can if anyone has any questions as to what my question is.
As always, any help is really appreciated.
Thanks, Dan

채택된 답변

Chandra Kurniawan
Chandra Kurniawan 2011년 12월 16일
Hello, Dan
I have a function file named 'fun1.m' stored in any folder.
The function 'fun1' takes 2 input argument [x and y].
function fun1(x,y)
disp(x+y);
And then I have a script that will read this function.
You can select that via uigetfile. And you need to input first and second function paramater.
Here the script. Just try to run it.
clear; clc;
[Fn Path] = uigetfile('*.m','Select m-file');
addpath(Path);
Fn = regexprep(Fn,'.m','');
hnd = str2func(Fn);
var1 = input('Input 1st parameter : ');
var2 = input('Input 2nd parameter : ');
hnd(var1,var2);
  댓글 수: 1
Dan
Dan 2011년 12월 16일
Thank you so much for your clear answer. It works absolutely perfectly.

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by