calling function from separate document for fitting

Hi all, I am working on this curve fitting project and I am trying out different methods to optimise the run time. One of the methods I am trying involes having 2 seperate scripts, with one script containing the function used for fitting and curve fitting (call this script 1), and the other script containing codes to load the data and process the data (call this script 2). However, when I try to call script 1 from 2, I get a ton of error messages. Could I please ask why is this the problem? Thank you! My codes are attached below:
tic
%% Preparation
clc; clear
format short
TAdata = readmatrix("FCPIB-293K-2.5mW-400nm-Jan072021 -ibg -bg -chirp.csv"); % insert file path within parenthesis
MBdata = readmatrix("carrier temp.xlsx");
P = load("Steady_State_Parameter_Values.mat");
Function = 'Transient_Absorption';
%% Preamble
% Fundamental constants
h = 4.1356677*10^-15; % units: eV/ Hz
c = 3*10^8; % SI units
kB = 8.617333268*10^-5; % units: eV/ K
% Data
Wavelength = TAdata(:, 1);% units: nm
E = (h*c)./(Wavelength*10^-9);
delay_t = TAdata(1, :);
delay_T = MBdata(:, 2);
carrier_T = MBdata(:, 3);
% Data for fitting
min_E = 1.5;
max_E = 2.0;
Range_E = E >= min_E & E <= max_E;
Range_W = Wavelength >= (h*c)/(max_E*10^-9) & Wavelength <= (h*c)/(min_E*10^-9);
E_p = E(Range_E); % selected probe energies
for n = 1:length(carrier_T)
a(1, n) = find(delay_T(n) == delay_t);
data_new2 = TAdata(Range_W, a);
end
%% Data for fitting and calling fitting function & solver
for i = 1:length(delay_T)
p = P.p;
A1 = p(1,1);
A2 = p(1,2);
Eg = p(1,3);
Eb = p(1,4);
R = p(1,5);
g = p(1,6);
deltaAbs = data_new2(:, i);
lb = [0, 0, 0, 0.3, 0, 0]; ub = [20, 0.05, 0.05, 2, 2, 1];
x0 = [1.6244, 0.0346, 0.03, 1.5139, 1, 0.3];
carrierT = carrier_T(i);
[x] = TA_fitting_function(A1, A2, Eg, Eb, R, g, x0,x,carrierT,E_p,deltaAbs,lb,ub,i,LegendText, Function);
end
Unrecognized function or variable 'x'.
%% Table of parameter values
time_delay = ['0.5 ps'; '1.0 ps'; '2.0 ps'; '4.0 ps'];
Eg_prime = x(:, 1);
Eb_prime = x(:, 2);
g_prime = x(:, 3);
Ef_q = x(:, 4);
f1 = x(:, 5);
f2 = x(:, 6);
T = table(time_delay, Eg_prime, Eb_prime, g_prime, Ef_q, f1, f2);
disp(T)
toc

 采纳的回答

Torsten
Torsten 2024-7-19
移动:Torsten 2024-7-19
However, when I try to call script 1 from 2, I get a ton of error messages. Could I please ask why is this the problem?
You can't call scripts, you can only call functions.
If you define EM_TA_wR_CC and EM_TA_wR_EC as function handles depending on x and E_p (as you use them in the plot commands), you don't need to pass x to "TA_fitting_function", and the error will disappear.

5 个评论

Hi, SFLR. I see. But what if I were to not use function handles? And I think I might have made my code confusing. I am actually not passing x into TA_fitting_function, but instead using TA_fitting_function to solve for x.
What exactly is your question ?
I should've been more specific. In the script named "TA_fitting_function", it contains the function that I am using for my curve fitting as well as the curve fitting options (I'm using lsqcurvefit) as well as plot commands. What I am trying to do now is to input my data on one of the scripts, and then use the stuff in "TA_fitting_function". But right now I can't seem to make that work...
In the script named "TA_fitting_function"
At the moment, TA_fitting_function is not a script, but a function. And as far as I know, it's not possible to run several scripts simultaneously in MATLAB.
What I am trying to do now is to input my data on one of the scripts, and then use the stuff in "TA_fitting_function".
Define the things you want to use in "TA_fitting_function" and that you think should be set before in your script and call "TA_fitting_function" with these inputs. You can even define the name of the function you want to use for fitting as a function name as
[sol] = TA_fitting_function(@fun,other inputs)
I see. Will definitely give it a try. Thank you!

请先登录,再进行评论。

更多回答(1 个)

Hi @Jack, the error that you are recieving is because you are passing 'x' as an argument of 'TA_fitting_function' but not defining it anywhere in the script.
Hope that helps!

类别

帮助中心File Exchange 中查找有关 Get Started with Curve Fitting Toolbox 的更多信息

产品

版本

R2024a

标签

Community Treasure Hunt

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

Start Hunting!

Translated by