How to run m-files(main function is variable) with Matlab code?

12 次查看(过去 30 天)
The values of variable have been got by excel file. And also known the name of m-file for the main function. I want to call this main function with variable or others instead of type the function name directly.
for example
below inputs already defined.
a1=1;
a2=2;
a3=5;
b1=6;
b2=9;
...
Question is how to run the m-file with above inputs value? i try to use
run myfun
but error happen due to variable a1, a2, ...b2 don't be assigned with above values.
I know we can use below comen code. but it is not what I want because there are many different m-file. I want to make a general coe to replace below code. I don't want to copy below code flow and past it every time for too many m-file.
[x1,x2,x3]=myfun(a1 a2 a3 b1 b2 )
below function is defined in myfun.m
function [x1,x2,x3]=myfun(a1 a2 a3 b1 b2 )
x1=a1+a2,
x2=a3+a2;
x3=b1+b2+b3;
end
  3 个评论
W. Feng
W. Feng 2020-5-19
I want to make a generic program to run the main function instead of copy and past all the argument inputs.
argument_input1=5;
argument_input2=6;
...
argument_input30003=9;
Input_parameter=[5 6 ... 9]
above arguments could be generate easily by some codes because they are from other file, the name and its values are availble .
the core function is also available. And basically operation is like below to run main function
S=myfun(argument_input1, argument_input2, argument_input3, ..., argument_input3003 )
or
S=myfun(5,6,...,9)
as you see there are many many arguments. I hope it could make it with other code.
If i use below command. Input_parameter just assign to the first argument. I know it is also easy to copy and past this to run. I hope the whole calcualtion without any copy and past. Just run it and select input file and then select the M-file of main function. And it is done.
feval(funname, Input_parameter);
how to make the arguments pass main function. Thank you.
Do you have any idear for this?
Rik
Rik 2020-5-19
It looks to me like your numbered variables should be arrays instead. Otherwise you would have to do something useful with 30k input variables. That isn't impossible, but it is not likely to be what you need.

请先登录,再进行评论。

采纳的回答

Ameer Hamza
Ameer Hamza 2020-5-19
编辑:Ameer Hamza 2020-5-19
You can use the cell array to expand the input arguments automatically. For example
argument_input1=5;
argument_input2=6;
...
argument_input30003=9;
Input_parameter={5 6 .. 9} % cell array
Call the function like this
S=myfun(Input_parameter{:})

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Language Fundamentals 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by