How to define data input in an elegant way?
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commento
Stephen23
il 23 Feb 2017
@Xh Du: that looks like a very reasonable way to specify input data. What is your concern?
Risposte (1)
Adam
il 23 Feb 2017
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 Commenti
Adam
il 23 Feb 2017
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?
Stephen23
il 23 Feb 2017
@Xh Du: avoid scripts. Functions and classes make code much more robust:
Vedere anche
Categorie
Scopri di più su Data Type Identification in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!