How to load parameters in different format?
Mostra commenti meno recenti
Hi, I'm interested in code optimization. Since I have many parameters, vectors and matrices to be initialized in my code, I'm looking for another formats (binary?) or strategies to load them.
How can I do it?
Thank you!
7 Commenti
dpb
il 21 Lug 2022
What do you call "many"?
Think would need a lot more description of what you're actually trying to model to have any real input...but in general, one can either compute those quantities that can be defined programmatically with some algorithm or read files of essentially any format one wishes to create ('LOAD/SAVE' are particularly convenient/easy in MATLAB if you have the data in ML from somewhere first) or hardcode in constants as arrays or other higher-level abstractions in one or more m-files that are called as functions to return their values. Something like 'pi' is a builtin function that simply returns a constant. The problem with that solution can be one of how many return variables one may need in the calling function.
IOW, "it all depends..."
Elia Paini
il 25 Lug 2022
dpb
il 25 Lug 2022
Again, how many and are these specific to a given code/function and are they truly constant or [semi]variable depending on the case?
If they're fixed, permanent and belong to the particular function, then I'd just code them in the once time and be done with it. I tend to follow the old-timey practice that many constants use all uppercase variable names as a reminder.
It's unfortunate MATLAB didn't bring over the PARAMETER statement with its Fortran initial heritage (along with FORMAT).
Elia Paini
il 25 Lug 2022
dpb
il 25 Lug 2022
Then they're really NOT constants but data.
I'd probably use a text file and a specific, easily parseable and identifiable format to identify "who's who in the zoo" and read them in -- unless there's the possibility of there being a fixed number of cases in which case one could create a lookup table from which to select a given set.
But, if they're just variable-valued parameters that can change essentially arbitrarily, then just editing them externally is probably the easiest route.
Editing in m-files is more effort and requires changing names to keep multiple copies to call; that's what data files are for.
Elia Paini
il 26 Lug 2022
Modificato: Elia Paini
il 26 Lug 2022
That all depends on how you organize your file -- again, without more specifics, it's hard to hypothesize particular solutions...that's the thing about writing code -- it's all in the details; generalities don't produce design specifications.
It also all depends on what kind of a tool you're building -- if you're just working at command line mostly and calling some user functions, perhaps the workspace variable editor could be just the thing to use...or, if it's to be a fully-packaged compiled app for general distribution, then it is probably necessary to design the data import/export facilities to match.
We simply do not know...
<data-import-and-export> is link to top-level section that describes all the possibilities and outlines various standard formats that could choose from...see what tickles your fancy/matches your idea of how you would like to interact with the program you're building.
Risposte (0)
Categorie
Scopri di più su Debugging and Analysis in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!