Saving a Script from another script

2 次查看(过去 30 天)
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

回答(2 个)

ScottB
ScottB 2023-12-14
编辑:ScottB 2023-12-14
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.

Image Analyst
Image Analyst 2023-12-14
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

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by