How to make input parameters in m.file and use another function file to write the code, and ask paramters from m.file?
56 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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?
0 commentaires
Réponses (2)
Thorsten
le 22 Juil 2016
Modifié(e) : Thorsten
le 22 Juil 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 commentaires
Thorsten
le 22 Juil 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
le 23 Juil 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.
dpb
le 21 Juil 2016
Modifié(e) : dpb
le 22 Juil 2016
Think of two possible solutions--
- 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.
- 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.
0 commentaires
Voir également
Catégories
En savoir plus sur Workspace Variables and MAT-Files dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!