Error I don't understand "undefined variable andre_tilgjengelig"

1 次查看(过去 30 天)
What's wrong with the attached code? When I run the function I get the message
"undefined variable og function andre_tilgjengelig"
(Sorry that I am posting a long code, but right now I am feeling a little helpless.)
function [maks_vektprosent_lost_CaCl2, maks_vektprosent_lost_MgCl2,maks_vektprosent_lost_NH4Cl] = beregn_maks_vektprosent_lost_1(forste_saltet, andre_saltet, tredje_saltet)
if forste_saltet==1
forste_tilgjengelig='NH4Cl';
elseif andre_saltet==1
andre_tilgjengelig='NH4Cl';
elseif tredje_saltet==1
tredje_tilgjengelig='NH4Cl';
elseif forste_saltet==2
forste_tilgjengelig='MgCl2';
elseif andre_saltet==2
andre_tilgjengelig='MgCl2';
elseif tredje_saltet==2
tredje_tilgjengelig='MgCl2';
elseif forste_saltet==3
forste_tilgjengelig='CaCl2';
elseif andre_saltet==3
andre_tilgjengelig='CaCl2';
elseif tredje_saltet==3
tredje_tilgjengelig='CaCl2';
end
str={'Enter the temperature you want to compute the weight percent with'};
S={' 10 C';' 20 C';' 30 C';' 40 C';' 50 C';' 60 C';' 70 C';' 80 C';' 90 C';'100 C'};
result=listdlg('Promptstring',str, 'ListSize', [400,400], 'ListString', S, 'SelectionMode', 'single');
if strcmpi(forste_tilgjengelig, 'CaCl2')||strcmpi(andre_tilgjengelig, 'CaCl2')||strcmpi(tredje_tilgjengelig, 'CaCl2')
if (result==1)
maks_vektprosent_lost_CaCl2=(64.7/(64.7 + 100))*100;
disp('weight percent of CaCl2 when the temperaure is 10 celcius is:')
disp(maks_vektprosent_lost_CaCl2)
elseif (result==2)
maks_vektprosent_lost_CaCl2=(74.5/(74.5 +100))*100;
disp('weight percent of CaCl2 when the temperaure is 20 celcius is:')
disp(maks_vektprosent_lost_CaCl2)
elseif (result==3)
maks_vektprosent_lost_CaCl2=(100/(100 + 100))*100;
disp('weight percent of CaCl2 when the temperaure is 30 celcius is')
disp(maks_vektprosent_lost_CaCl2)
elseif (result==4)
maks_vektprosent_lost_CaCl2=(128/(128 + 100))*100;
disp('weight percent of CaCl2 when the temperaure is 40 celcius is')
disp(maks_vektprosent_lost_CaCl2)
elseif (result ==5)
maks_vektprosent_lost_CaCl2=(128.8/(128.8+100))*100;
disp('weight percent of CaCl2 when the temperaure is 50 celcius is')
disp(maks_vektprosent_lost_CaCl2)
elseif (result==6)
maks_vektprosent_lost_CaCl2=(137/(137 + 100))*100;
disp('weight percent of CaCl2 when the temperaure is 60 celcius is')
disp( maks_vektprosent_lost_CaCl2)
elseif (result==7)
maks_vektprosent_lost_CaCl2= (143.45/(143.45 + 100))*100;
disp('weight percent of CaCl2 when the temperaure is 70 celcius is')
disp(maks_vektprosent_lost_CaCl2)
elseif (result==8)
maks_vektprosent_lost_CaCl2=( 147/(147+ 100))*100;
disp('weight percent of CaCl2 when the temperaure is 80 celcius is')
disp(maks_vektprosent_lost_CaCl2)
elseif (result==9)
maks_vektprosent_lost_CaCl2= (154/(154+100))*100;
disp('weight percent of CaCl2 when the temperaure is 90 celcius is')
disp(maks_vektprosent_lost_CaCl2)
elseif (result==10)
maks_vektprosent_lost_CaCl2= (159/(159 +100))*100;
disp('weight percent of CaCl2 when the temperaure is 10 celcius is')
disp(maks_vektprosent_lost_CaCl2)
end
else
maks_vektprosent_lost_CaCl2=[];
end
if strcmpi(forste_tilgjengelig, 'MgCl2')||strcmpi(andre_tilgjengelig, 'MgCl2')||strcmpi(tredje_tilgjengelig,'MgCl2')
if (result==1)
maks_vektprosent_lost_MgCl2=(53.6/(53.6+100))*100;
disp('weight percent of MgCl2 when the temperaure is 10 celsius is')
disp(maks_vektprosent_lost_MgCl2)
elseif (result==2)
maks_vektprosent_lost_MgCl2=(54.6/(54.6+100))*100;
disp('weight percent of MgCl2 when the temperaure is 20 celsius is')
disp(maks_vektprosent_lost_MgCl2)
elseif (result==3)
maks_vektprosent_lost_MgCl2=(55.8/(100+55.8))*100;
disp('weight percent of MgCl2 when the temperaure is 30 celsius is')
disp(maks_vektprosent_lost_MgCl2)
elseif (result==4)
maks_vektprosent_lost_MgCl2=(57.5/(57.5+100))*100;
disp('weight percent of MgCl2 when the temperaure is 40 celsius is')
disp(maks_vektprosent_lost_MgCl2)
elseif (result==5)
maks_vektprosent_lost_MgCl2=(59.01/(100+59.01))*100;
disp('weight percent of MgCl2 when the temperaure is 50 celsius is')
disp(maks_vektprosent_lost_MgCl2)
elseif (result==6)
maks_vektprosent_lost_MgCl2=(61/(61+100))*100;
disp('weight percent of MgCl2 when the temperaure is 60 celsius is')
disp(maks_vektprosent_lost_MgCl2)
elseif (result==7)
maks_vektprosent_lost_MgCl2=(63.67/(63.67+100))*100;
disp('weight percent of MgCl2 when the temperaure is 80 celsius is')
disp(maks_vektprosent_lost_MgCl2)
elseif (result==8)
maks_vektprosent_lost_MgCl2=(66.1/(66.1+100))*100;
disp('weight percent of MgCl2 when the temperaure is 90 celsius is')
disp(maks_vektprosent_lost_MgCl2)
elseif (result==9)
maks_vektprosent_lost_MgCl2=(69.5/(100+69.5))*100;
disp('weight percent of MgCl2 when the temperaure is 90 celsius is')
disp(maks_vektprosent_lost_MgCl2)
elseif (result==10)
maks_vektprosent_lost_MgCl2=(73.3/(73.3+100))*100;
disp('weight percent of MgCl2 when the temperaure is 100 celsius is')
disp(maks_vektprosent_lost_MgCl2)
end
maks_vektprosent_lost_MgCl2=[];
end
if strcmpi(forste_tilgjengelig, 'NH4Cl')||strcmpi(andre_tilgjengelig, 'NH4Cl')||strcmpi(tredje_tilgjengelig, 'NH4Cl')
if (result==1)
maks_vektprosent_lost_NH4Cl= (33.2/(33.2+100))*100;
disp('Weight percent of NH4Cl when the temperature is 10 celsius is')
disp( maks_vektprosent_lost_NH4Cl)
elseif (result==2)
maks_vektprosent_lost_NH4Cl=(37.2/(100+37.2))*100;
disp('Weight percent of NH4Cl when the temperature is 20 celsius is')
disp( maks_vektprosent_lost_NH4Cl)
elseif (result==3)
maks_vektprosent_lost_NH4Cl=(41.4/(100+41.4))*100;
disp('Weight percent of NH4Cl when the temperature is 30 celsius is')
disp( maks_vektprosent_lost_NH4Cl)
elseif (result==4)
maks_vektprosent_lost_NH4Cl=(45.8/(100+45.8))*100;
disp('Weight percent of NH4Cl when the temperature is 40 celsius is')
disp( maks_vektprosent_lost_NH4Cl)
elseif (result==5)
maks_vektprosent_lost_NH4Cl=(50.4/(50.4+100))*100;
disp('Weight percent of NH4Cl when the temperature is 50 celsius is')
disp( maks_vektprosent_lost_NH4Cl)
elseif (result==6)
maks_vektprosent_lost_NH4Cl=(55.3/(100+55.3))*100;
disp('Weight percent of NH4Cl when the temperature is 60 celsius is')
disp( maks_vektprosent_lost_NH4Cl)
elseif (result==7)
maks_vektprosent_lost_NH4Cl=(60.2/(60.2+100))*100;
disp('Weight percent of NH4Cl when the temperature is 70 celsius is')
disp( maks_vektprosent_lost_NH4Cl)
elseif (result==8)
maks_vektprosent_lost_NH4Cl=(65.6/(65.6+100))*100;
disp('Weight percent of NH4Cl when the temperature is 80 celsius is')
disp( maks_vektprosent_lost_NH4Cl)
elseif (result==9)
maks_vektprosent_lost_NH4Cl=(71.2/(71.2+100))*100;
disp('Weight percent of NH4Cl when the temperature is 90 celsius is')
disp(maks_vektprosent_lost_NH4Cl)
elseif (result==10)
maks_vektprosent_lost_NH4Cl=(77.3/(100+77.3))*100;
disp('Weight percent of NH4Cl when the temperature is 100 celsius is')
disp( maks_vektprosent_lost_NH4Cl)
end
maks_vektprosent_lost_NH4Cl=[];
end
end

回答(2 个)

Image Analyst
Image Analyst 2021-12-18
Step through the code with the debugger and you'll see it never gets defined.
Look at this link and then step through your program one line at a time.
You can initialize variables if you want.
function [maks_vektprosent_lost_CaCl2, maks_vektprosent_lost_MgCl2, maks_vektprosent_lost_NH4Cl] = ...
beregn_maks_vektprosent_lost_1(forste_saltet, andre_saltet, tredje_saltet)
% Initialize some variables:
forste_tilgjengelig='?';
andre_tilgjengelig='?';
tredje_tilgjengelig='?';
  5 个评论
Muazma Ali
Muazma Ali 2021-12-21
编辑:Image Analyst 2021-12-21
No debugging of each line, and then stopping there. This alternative is not found in MATLAB 2013.
Image Analyst
Image Analyst 2021-12-21
Nonsense. I've used MATLAB since R2006a - every version. And debugging has been available in every single version. What happens if you click on the line number in the editor window and say run? It should put a red spot there over the line number and when your code gets there, it will stop.
You said "I just need to download it" meaning r2020. Don't do that. Might as well get the latest version which is now r2021b. So did you do that?

请先登录,再进行评论。


Voss
Voss 2021-12-18
编辑:Voss 2021-12-18
Your first if/elseif/elseif block looks like this:
if forste_saltet==1
forste_tilgjengelig='NH4Cl';
elseif andre_saltet==1
andre_tilgjengelig='NH4Cl';
elseif tredje_saltet==1
tredje_tilgjengelig='NH4Cl';
elseif forste_saltet==2
forste_tilgjengelig='MgCl2';
elseif andre_saltet==2
andre_tilgjengelig='MgCl2';
elseif tredje_saltet==2
tredje_tilgjengelig='MgCl2';
elseif forste_saltet==3
forste_tilgjengelig='CaCl2';
elseif andre_saltet==3
andre_tilgjengelig='CaCl2';
elseif tredje_saltet==3
tredje_tilgjengelig='CaCl2';
end
So that, for instance, if forste_saltet == 1, then forste_tilgjengelig is set to 'NH4Cl', but andre_tilgjengelig and tredje_tilgjengelig do not get set (because none of the remaining elseif blocks are executed). This type of behavior is the source of the error. You probably mean to have three independent if/elseif blocks - one for each variable. Like this:
if forste_saltet==1
forste_tilgjengelig='NH4Cl';
elseif forste_saltet==2
forste_tilgjengelig='MgCl2';
elseif forste_saltet==3
forste_tilgjengelig='CaCl2';
end
if andre_saltet==1
andre_tilgjengelig='NH4Cl';
elseif andre_saltet==2
andre_tilgjengelig='MgCl2';
elseif andre_saltet==3
andre_tilgjengelig='CaCl2';
end
if tredje_saltet==1
tredje_tilgjengelig='NH4Cl';
elseif tredje_saltet==2
tredje_tilgjengelig='MgCl2';
elseif tredje_saltet==3
tredje_tilgjengelig='CaCl2';
end
Now each variable will get a value based on the corresponding value of the input variable (as long as it is 1, 2, or 3 - if not the same type of error could occur).

类别

Help CenterFile Exchange 中查找有关 Computer Vision with Simulink 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by