Run another script via run parameter
6 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Hello,
I have the following issue:
I have different data sets that I define in different files, for example:
fileA.m:
var1 = 5;
var2 = 7;
fileB.m:
var1 = -9;
var2 = 15;
And then I have my main script, in which I need to do something with the var1 and va2 variables. At the time of the calling of the main script, I would like to choose which prior script to run first to populate the workspace with the right var1 and var2. For now, In my main script I call the needed script by hand at the beginning, but that requires me to edit the script content each time I want to change the "database".
What is the best way to solve this? A mat file does not help, as it is a binary file...
I would appreciate all help!
EDIT: I notice that I can also just run scripts one by one, i.e.:
fileA, myEndScript
But not sure whether thats the optimal way for this.
0 Commenti
Risposte (1)
Jan
il 24 Mar 2022
The best way is to avoid scripts and to use functions instead. Scripts increase the complexity of the code, because side effects can appear and influence the behaviour of the program over a long distance. This impedes the debugging.
Example:
% Script 1:
a = zeros(1, 10);
...
% Script 2:
for k = 1:5
a(k) = k;
end
a(end) % This is not the 5th element!
Using functions keeps the workspaces clear and avoid unexpected side-effects.
You can store both data sets in one function:
function data = DataSet(Name)
switch Name
case 'Set 1'
data.var1 = 5;
data.var2 = 7;
case 'Set 2'
data.var1 = -9;
data.var2 = 15;
end
end
Now provide the name of the wanted data set to the main function:
function main(DataName)
data = DataSet(DataName)
...
end
Now you can control dynamically, which data are used and do not have to modify the code.
Use variables to access data dynamically. Editing scripts is an indirection to do this. You know the concept of using functions from all of Matlab's toolboxes. Scripts are useful for short hacks only, while productive code should not contain any scripts.
7 Commenti
Jan
il 24 Mar 2022
Do not provide "data bases" as Matlab scripts. Use binary or text files instead, MAT files or the professional approach is a real data base, of course.
A patchwork of scripts is the horror for productive work. As soon as several persons should this this code, the scripts will get muddy very soon.
Vedere anche
Categorie
Scopri di più su Database Toolbox 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!