MATLAB: Opening a file and loading a function from another script

functionfunctionsuigetfile

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

Best Answer

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);