필터 지우기
필터 지우기

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 CenterFile Exchange에서 Debugging and Analysis에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by