Subroutine functions in MatLab
Show older comments
How can I make a subroutine module (similar to another programming languages) in a MatLab m-file program?
How to call it in case you no variables between the function brackets such as "function [ init ] = init( )"?
Answers (1)
James Tursa
on 9 Aug 2018
Edited: James Tursa
on 9 Aug 2018
Create a file with the following name: init.m
Put your code into this file, e.g.,
function stuff = init
stuff = whatever;
end
Call this function like you would any other function:
mystuff = init;
12 Comments
Mohamed Agha
on 9 Aug 2018
James Tursa
on 9 Aug 2018
If you are talking about separating your main program into several files, but have all of those files operate on variables in your main program workspace, then you are talking about creating scripts ... which are basically m-code that doesn't have a function line at the top. Having your main program split into multiple script files is a poor way to program IMO. If your main program seems to be too large to manage all at once, then having modules that take inputs and do some operations and return outputs is a better way to split your program into multiple files.
What is the motivation for creating these "modules"?
Mohamed Agha
on 9 Aug 2018
Walter Roberson
on 9 Aug 2018
You can do the same in MATLAB.
If you need the equivalent of a COMMON block then use global. There are, however, typically better approaches than global.
Stephen23
on 9 Aug 2018
"I just want to separate the main m-file program into some modules (subroutine or subprogram)"
This will turn into spaghetti code. Experienced MATLAB users avoid doing this, and give advice to avoid doing this. Scripts are best avoided for any kind of serious work, where testability and repeatability are important.
"I don't want to return a value, I don't want to assign the function to a value..."
You might not like the advice that you get, but if you use functions and pass arguments properly your code will be easier to write, easier to debug, easier to document, easier to maintain, etc.
James Tursa
on 9 Aug 2018
Edited: James Tursa
on 9 Aug 2018
"... and contain multiple long subroutines to be executed in the main program whenever there is a need to do so ..."
That's fine, but what do the interfaces to these routines look like? No argument list but only COMMON blocks for input/output? Or what?
E.g., if it is COMMON blocks, you could stuff all of the COMMON block variables into a struct. Then pass that struct in & out of your "module" code via the argument list. Your "module" code could use/modify the struct fields etc.
Mohamed Agha
on 9 Aug 2018
Edited: Mohamed Agha
on 9 Aug 2018
"Is it possible! I think it will be more complex as the variables aren't of the same matrix type (some are constants, vectors, arrays"
A structure can hold variables of any types and sizes. A structure can be non-scalar, and have any number of fields. If you need to pass a large number of parameters/values at once, then a structure is perfect.
James Tursa
on 9 Aug 2018
E.g., suppose your Fortran code was something like this:
PROGRAM MAIN
REAL*8 X(4,5)
INTEGER K
COMMON /STATES/ X,K
CALL STATES_INIT
CALL STATES_WORK
:
END
SUBROUTINE STATES_INIT
REAL*8 X(4,5)
INTEGER K
COMMON /STATES/ X,K
! some code here to place initial values in X and K
RETURN
END
SUBROUTINE STATES_WORK
REAL*8 X(4,5)
INTEGER K
COMMON /STATES/ X,K
! some code here to modify X and K
RETURN
END
Then your MATLAB code could look something like this:
my_states = states_init;
my_states = states_work(my_states);
with functions
function states = states_init
states.x = whatever; % could be scalar, vector, matrix etc.
states.k = whatever; % could be scalar, vector, matrix etc.
end
function states = states_work(states)
states.x = some calculation that updates this value
states.k = some calculation that updates this value
end
Mohamed Agha
on 9 Aug 2018
Rajasmita Sahoo
on 20 Jan 2023
please kindly reply.
Walter Roberson
on 20 Jan 2023
Please see http://matlab.wikia.com/wiki/FAQ#How_can_I_share_data_between_callback_functions_in_my_GUI.28s.29.3F for information on better approaches than using global
Categories
Find more on Performance and Memory in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!