Pass a structure to a function

24 ビュー (過去 30 日間)
George Bashkatov
George Bashkatov 2021 年 2 月 28 日
コメント済み: George Bashkatov 2021 年 4 月 4 日
I'm trying to pass this structure to the function, but matlab writes: Dot indexing is not supported for variables of this type. Error in famplifire (line 3). How I can fix that?
function dydz = famplifire(Par,k,y,z) %Function with parameters to be passed
global Par;
k=Par.N0*Par.sigma_pa/(Par.sigma_pa+Par.sigma_pe);
dy1 = y(1).*(-Par.sigma_pa.*Par.N0+(Par.sigma_pa+Par.sigma_pe).*(k.*y(1)./(y(1)+y(2)+1)));
dy2 = y(2).*Par.sigma_se.*(k.*y(1)./(y(1)+y(2)+1));
dydz = [dy1; dy2];
And a part of main code:
global Par;
Par=struct;
Par.sigma_pa=0.6*10^(-18)*10^(-4);
Par.N0=1*10^19*10^6;
Par.sigma_pe=0.3*10^(-18)*10^(-4);
Par.sigma_se=0.9*10^(-18)*10^(-4);
k=Par.N0*Par.sigma_pa/(Par.sigma_pa+Par.sigma_pe);
________________________________________________________________
zspan=[0 l];
startval=[b; a];
[z1,y1]=ode45(@(z,y) famplifire(Par,k,y,z),zspan,startval);

採用された回答

Walter Roberson
Walter Roberson 2021 年 4 月 3 日
zspan=[0 l];
startval=[b; a];
You did not define l or a or b in what you posted.
function dydz = famplifire(Par,k,y,z) %Function with parameters to be passed
global Par;
The Par that is passed in is being replaced by the global par. In a release soon, it will simply be an error to use global with the same variable name as a parameter.
function dydz = famplifire(Par,k,y,z) %Function with parameters to be passed
k=Par.N0*Par.sigma_pa/(Par.sigma_pa+Par.sigma_pe);
You pass in k, but you immediately overwrite it.
I did not encounter the problem you indicate, but like @Robert U indicated, that problem could be caused by using global.
  2 件のコメント
Walter Roberson
Walter Roberson 2021 年 4 月 3 日
Par=struct;
Par.sigma_pa=0.6*10^(-18)*10^(-4);
Par.N0=1*10^19*10^6;
Par.sigma_pe=0.3*10^(-18)*10^(-4);
Par.sigma_se=0.9*10^(-18)*10^(-4);
k=Par.N0*Par.sigma_pa/(Par.sigma_pa+Par.sigma_pe);
l = 1e-2;
b = 1;
a = 2;
zspan=[0 l];
startval=[b; a];
[z1,y1]=ode45(@(z,y) famplifire(Par,k,y,z),zspan,startval);
plot(z1, y1)
function dydz = famplifire(Par,k,y,~) %Function with parameters to be passed
dy1 = y(1).*(-Par.sigma_pa.*Par.N0+(Par.sigma_pa+Par.sigma_pe).*(k.*y(1)./(y(1)+y(2)+1)));
dy2 = y(2).*Par.sigma_se.*(k.*y(1)./(y(1)+y(2)+1));
dydz = [dy1; dy2];
end
George Bashkatov
George Bashkatov 2021 年 4 月 4 日
thank you a lot

サインインしてコメントする。

その他の回答 (1 件)

Robert U
Robert U 2021 年 2 月 28 日
編集済み: Robert U 2021 年 3 月 4 日
Hi George Bashkatov,
First of all: Do not use global variables if not absolutely necessary. There are hundreds of threads arguing global variable troubles that are easily avoided by not using global variables.
Most propably your global variable assignment mixed something up. Neither could I reproduce the error nor could I execute your code directly. Please, make sure that all variables are supplied when posting code.
My suggestion is to perform a input test of your function inputs:
function dydz = famplifire(Par,k,y,z) %Function with parameters to be passed
validateattributes(Par,{'struct'},{'nonempty'});
cFieldnames = fieldnames(Par);
cNeededFieldnames = {'N0', 'sigma_pa', 'sigma_pe'};
if ~all(ismember(cNeededFieldnames,cFieldnames))
error('Variable ''Par'' does not supply all needed fields.')
end
k=Par.N0*Par.sigma_pa/(Par.sigma_pa+Par.sigma_pe);
dy1 = y(1).*(-Par.sigma_pa.*Par.N0+(Par.sigma_pa+Par.sigma_pe).*(k.*y(1)./(y(1)+y(2)+1)));
dy2 = y(2).*Par.sigma_se.*(k.*y(1)./(y(1)+y(2)+1));
dydz = [dy1; dy2];
end
Kind regards,
Robert
  1 件のコメント
Walter Roberson
Walter Roberson 2021 年 4 月 3 日
that doesn't solve my problem

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by