필터 지우기
필터 지우기

How to define data input in an elegant way?

조회 수: 3 (최근 30일)
Xh Du
Xh Du 2017년 2월 23일
댓글: Stephen23 2017년 2월 23일
Hi all,
I have a newbie question: in complex programs, we may have lots of input data like this (for instance in my program):
time.max = 5;
time.step = 1;
no.incl = 3;
no.pre.hat = 4;
fce.time = 1;
fce.node = 4;
domain.length.I1 = 17;
domain.length.I2 = 17;
domain.length.S = 17;
domain.bond.L.I1 = 1;
domain.bond.R.I1 = 2;
domain.bond.L.I2 = 1;
domain.bond.R.I2 = 2;
pmVal.fix.I3 = 1000;
and some empty sets like this:
err.max.store = [];
err.loc.store = [];
err.max.store_exactwRB = [];
err.loc.store_exactwRB = [];
err.max.store_hat = []; % ehat
err.loc.store_hat = [];
err.max.store_hhat = []; % ehhat
err.loc.store_hhat = [];
I have a feeling that this is extremely inelegant and unnecessary, also hard to read. Is there any ways to define a big data input elegantly and efficiently?
Many thanks!
  댓글 수: 1
Stephen23
Stephen23 2017년 2월 23일
@Xh Du: that looks like a very reasonable way to specify input data. What is your concern?

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

답변 (1개)

Adam
Adam 2017년 2월 23일
Grouping data together like that is certainly far better than hundreds of individual variables.
I tend to use classes myself, but purely from the point of view of grouping data together structs work equally. Make sure your variable, struct, field names are as clear and intuitive as they can be though, even if they are quite long. Nobody wants to look at code with 27 variables all with names like ya, yb, ys, A, B, C1, C2, etc
Perhaps a bigger question is do you need all those things in the same place? Can you not break down your algorithm into smaller parts where you don't need all those things at once? Possibly you do at the very top level to feed them into the system in the first place, but later on parts of the program should be as self-contained as you can make them.
  댓글 수: 5
Adam
Adam 2017년 2월 23일
How does that script get called though? From a code readability and understandability point of view a function that returns 'time' (though preferably with a name that doesn't hide a builtin function) would make it clear that the variable exists within whichever function it was called from.
Calling a script from inside a function means that your function workspace will suddenly acquire whatever variables were in the script's workspace when it finishes and none of this is visually obvious in the function without opening the script and understanding it.
If
time.max = 5;
is literally all that is in a script though then surely it could just go in-place instead of the line to call the script?

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

카테고리

Help CenterFile Exchange에서 Data Type Identification에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by