Saving a Script from another script

3 vues (au cours des 30 derniers jours)
Sam Bottum
Sam Bottum le 14 Déc 2023
Hello - I am using an input script to run a main simulation file on a computing cluster. For this, I need to change one or two parameters in the input script before uploading it and running it again. This requires me to manually generate hundreds of input files. Is there a way to write a third script that changes 1-2 lines of code in the input script and then saves it as a new file?
I have attached my code - I need to change the diameter and concentration parameters and then resave each file as of now but it's quite slow as you can imagine...
% OPTIONS FOR RUN
main = 1;
scatter = 0;
eff_path = 0;
tic
load 'Si_n_complex' %loads complex Si refractive index data (lambda = 250-1000 nm)
global lambda diameter NW_conc bound_x bound_y bound_z n0 n1_all N_photon N_steps
n0=1.33; %Refractive index of surrounnding medium
n1_all=complex(Si_n(2,:),Si_k(2,:)); %Complex refractive index of dielectric
NW_num = 1e8; %NWs per mL
NW_conc = NW_num/1000; %putting conc into mm^-3 for script
diameter = 200;
bound_x = [0 10]; % sets -/+ x boundaries
bound_y = [-5 5]; % sets -/+ y boundaries
bound_z = [-22.5 22.5]; % sets -/+ z boundaries
if main == 1
lambda_all = [400e-9:1e-9:1200e-9];
N_photon_all = zeros(length(lambda_all),1);
counter_abs_all = zeros(length(lambda_all),1);
counter_side_all = zeros(length(lambda_all),1);
counter_back_all = zeros(length(lambda_all),1);
counter_trans_all = zeros(length(lambda_all),1);
N_photon = 1000; %number of photons to walk
N_steps = 10000; % maximum number of steps in walk
for qq = 1:length(lambda_all)
try
lambda = lambda_all(qq);
MC_main_v17
N_photon_all(qq) = N_photon;
counter_abs_all(qq) = counter_abs;
counter_back_all(qq) = counter_back;
counter_side_all(qq) = counter_side_exit;
counter_trans_all(qq) = counter_exit;
catch
break
end
end
file_name = mfilename + ".csv";
Q = [transpose(lambda_all),N_photon_all,counter_abs_all,counter_side_all,counter_back_all,counter_trans_all];
writematrix(Q,file_name)
elseif scatter == 1
lambda = [655e-9];
N_photon = 1000000; %number of photons to walk
N_steps = 200; % maximum number of steps in walk
MC_main_v17
file_name = mfilename;
Q1 = [transpose(side_exit_position_x),transpose(side_exit_position_y),];
Q2 = [transpose(back_exit_position_x),transpose(back_exit_position_y)];
Q3 = [transpose(trans_exit_position_x),transpose(trans_exit_position_y)];
writematrix(Q1,file_name+"_side");
writematrix(Q2,file_name+"_back");
writematrix(Q3,file_name+"_trans");
elseif eff_path == 1
lambda_all = [400e-9:10e-9:1200e-9];
N_photon_all = zeros(length(lambda_all),1);
counter_abs_all = zeros(length(lambda_all),1);
counter_side_all = zeros(length(lambda_all),1);
counter_back_all = zeros(length(lambda_all),1);
counter_trans_all = zeros(length(lambda_all),1);
eff_path_all = zeros(length(lambda_all),1);
N_photon = 1000; %number of photons to walk
N_steps = 10000; % maximum number of steps in walk
for qq = 1:length(lambda_all)
try
lambda = lambda_all(qq);
MC_main_v17
N_photon_all(qq) = N_photon;
counter_abs_all(qq) = counter_abs;
counter_back_all(qq) = counter_back;
counter_side_all(qq) = counter_side_exit;
counter_trans_all(qq) = counter_exit;
eff_path_all(qq) = mean(eff_path);
catch
break
end
end
file_name = mfilename + ".csv";
Q = [transpose(lambda_all),N_photon_all,counter_abs_all,counter_side_all,counter_back_all,counter_trans_all,eff_path_all];
writematrix(Q,file_name)
else
error('Run options not specified')
end
toc

Réponses (2)

ScottB
ScottB le 14 Déc 2023
Modifié(e) : ScottB le 14 Déc 2023
You can write or modify a script from script.
I've done it using fopen, fprint, and fclose commands. The example in the documentation for fprintf is pretty helpful.
  2 commentaires
Sam Bottum
Sam Bottum le 14 Déc 2023
I'll look into that. Thanks so much!
Image Analyst
Image Analyst le 14 Déc 2023
You most likely don't want to do this.

Connectez-vous pour commenter.


Image Analyst
Image Analyst le 14 Déc 2023
What you should do is to put all of your parameters that you want to vary in a text file or a workbook. Then read it in with something like readmatrix into an array. Then have a loop where you call all the code that needs to be run using the next row of data as parameters. For example
data = readmatrix(filename); % Read in 500 by 2 list of numbers.
% Get parameters from the columns of data.
var1 = data(:, 1); % Column 1
var2 = data(:, 2); % Column 2
for k = 1 : numel(var1)
thisVar1 = var1(k);
thisVar2 = var2(k);
% Now we have the parameters we need so run the
% actual code that does the computations with thisVar1 and thisVar2.
end

Catégories

En savoir plus sur Programming dans Help Center et File Exchange

Produits


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by