フィルターのクリア

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

40 ビュー (過去 30 日間)
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?

回答 (2 件)

Thorsten
Thorsten 2016 年 7 月 22 日
編集済み: Thorsten 2016 年 7 月 22 日
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 件のコメント
Thorsten
Thorsten 2016 年 7 月 22 日
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 2016 年 7 月 23 日
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
dpb 2016 年 7 月 21 日
編集済み: dpb 2016 年 7 月 22 日
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.

カテゴリ

Help Center および File ExchangeDebugging and Analysis についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by