필터 지우기
필터 지우기

Global variables (not sure if it's the case or not)

조회 수: 1 (최근 30일)
Antares
Antares 2017년 12월 4일
편집: Stephen23 2017년 12월 4일
Hi!
I have some numbers defined in the main script whose values are constant during all the simulation.
During the execution of the main I call several functions which take as input some variables and the same numbers defined before and as output some results, the problem is that passing all the numbers as parameters to the functions is quite annoying. Is there a way to bypass this? I tried with some global variables but it doesn't seem to work, I might have made some mistakes with declarations...
For Example (with only one number but I have many of them):
offset = 1;
result = myFunc(3)
%%%%%
function res = myFunc(val)
res = val + offset;
  댓글 수: 1
Stephen23
Stephen23 2017년 12월 4일
편집: Stephen23 2017년 12월 4일
Put constants and control variables (e.g. filenames, test IDs, etc) into one structure. This is trivial to pass and use. Do not use global variables.

댓글을 달려면 로그인하십시오.

채택된 답변

KL
KL 2017년 12월 4일
One idea is to use structs. Put all your variables under this struct and you'd need to pass only this struct. For example,
Param.offset = 1;
result = myFun(3,Param)
%%%%%
function res = myFunc(val,param)
res = val + param.offset;
I'd prefer this method than using globals.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by