Pass a structure to a function

조회 수: 27 (최근 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 CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by