Borrar filtros
Borrar filtros

How to make input parameters in m.file and use another function file to write the code, and ask paramters from m.file?

40 visualizaciones (últimos 30 días)
I have a project with a lots of input parameters and the code. I did all in one m.file, but I need to divide input parameters and codes into two different files.
I need to make two files:
m.file- will contain input parameters
function file - will contain codes
How to connect 2 files, and call parameters from m.file in function file and use it?
Could anyone help me?

Respuestas (2)

Thorsten
Thorsten el 22 de Jul. de 2016
Editada: Thorsten el 22 de Jul. de 2016
write a script parameters.m
alpha = 23;
beta = 44;
write a function
function z = myfunction(x, y)
% read script to define parameters
parameters
z = alpha*x + beta*y
  3 comentarios
Thorsten
Thorsten el 22 de Jul. de 2016
You can simply copy parameters.m to a parameters-descriptive-name.m and edit parameters.m before running your function.
This is no way less convenient than creating data files with descriptive names. The advantage of having a single file with all the parameters set my matlab commands is that you can easily edit them and have an overview of all the other parameters. But it is probably a matter of taste which way you prefer. Loading mat files may be faster than running m-files, but that may be relevant only for huge numbers of parameters.
dpb
dpb el 23 de Jul. de 2016
In naming files, no, not really a significant difference, agreed. It seems more effort to me to modify source than just data, however, but that's simply a style preference.

Iniciar sesión para comentar.


dpb
dpb el 21 de Jul. de 2016
Editada: dpb el 22 de Jul. de 2016
Think of two possible solutions--
  1. Simpler would be not to use m-file but a data file that contains the data and simply read it and use its content. save|load might be very useful for this as variables can be saved and retrieved automagically by name from the file.
  2. If were to insist on an m-file, it would need to contain a function whose job was to return a given set of variables when called to be assigned in the caller's workspace with which to do its subsequent work. This has the problem that multiple sets would require multiple m-files hence differing function names or have multiple sets of data with some selection logic to return a given set. All in all, much more awkward than the other option.

Categorías

Más información sobre Debugging and Analysis en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by