Subroutine functions in MatLab

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

I don't want to return a value, I don't want to assign the function to a value, i.e. I don't have this step "stuff = whatever;"
I just want to separate the main m-file program into some modules (subroutine or subprogram)
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"?
I am working in writing a long computational program which was originally written in Fortran programming language and contain multiple long subroutines to be executed in the main program whenever there is a need to do so
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.
"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
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
Mohamed Agha on 9 Aug 2018
Edited: Mohamed Agha on 9 Aug 2018
" .. No argument list but only COMMON blocks for input/output? Or what?"
Yes, there is a common block for all the variables in the main program and subroutines.
" if it is COMMON blocks, you could stuff all of the COMMON block variables into a struct."
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, ... etc)
"There are, however, typically better approaches than global."
How it can be formulated I am not experienced MatLab?
Stephen23
Stephen23 on 9 Aug 2018
Edited: Stephen23 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.
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
did that method work in your case @Mohamed Agha , whatever told by @James Tursa ?
please kindly reply.

Sign in to comment.

Categories

Asked:

on 9 Aug 2018

Commented:

on 20 Jan 2023

Community Treasure Hunt

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

Start Hunting!