MATLAB Answers


How to call/include/load one m file in another m file?

Asked by Micky
on 12 Apr 2013

I have a huge matlab program (.m file) which is a higher level model of some design and this program has bunch of variables. I want to store these variables in different/separate matlab file which should be .m file. So, I want in this way because I want to make changes with the variable values and run the original matlab program, basically run experiments with the model with new variable values. I know we can store varibles in mat file but it is not writable. So, basically my question comes down to this, I can store varibales in a separate m file but everytime I run the original program, how can I include and load the variables (m file) in the original matlab program file (m file)?


To run a .m file in another, just call the name of the .m file. For example, if you have two files A.m and B.m, to call B within A just type:

B;  %this will run B.m

Matt, why don't you move this to an answer?

Thanks Matt, it solved my problem.


No products are associated with this question.

1 Answer

Answer by per isakson
on 12 Apr 2013
Edited by per isakson
on 12 Apr 2013

There are many more or less smelly alternatives, one of which is a "data class". Example:

    classdef    MyInput
        properties ( Constant = true )
           p01 = 17; 

refer to the data in your huge program with

    >> MyInput.p01
    ans =

This approach helps make your code a bit more readable.

"... make changes with the variable values ..." - how many variable?

  1 Comment

Thanks per isakson!

Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!