The variable already defined ,but in the design apps it cannot recognized the variable.

Unrecognized function or variable 'A'.
Error in Tariff (line 21)
F6 = A+B+C;
function Bill = Tariff(Energy)
if Energy <=200
A = Energy*0.218;
B=0; C=0; D=0; E=0; P=0.4; EST= 0;
elseif Energy <=300
A = 43.6;
B = (Energy-200)*0.334;
C=0; D=0; E=0; P=0.15; EST= 0;
elseif Energy <=600
A = 43.6; B = 33.4;
C = (Energy-300)*0.516;
D=0; E=0; P=0.1; EST= 0;
elseif Energy <= 900
A = 43.6; B = 33.4; C = 154.8;
D = (Energy-600)*0.546;
E=0; P=0.05; EST= Energy-600;
elseif Energy > 900
A = 43.6; B = 33.4; C = 154.8; D = 327.6;
E = (Energy-900)*0.571; P=0; EST= Energy-600;
end
F6 = A+B+C;
ST = D+E;
Total = F6+ST;
ICPT = Energy*0.02;
ICPTST = EST*0.02;
Pemulih = round((Total-ICPT)*P,2);
PemulihST = round((ST-ICPTST)*P,2);
STtotal = round(ST-ICPTST-PemulihST,2);
Tax = round((STtotal)*0.06,2);
KWTBB = round((Total-Pemulih)*0.016,2);
Bill = round(Total-ICPT-Pemulih+Tax+KWTBB,1);

2 个评论

"The variable already defined"
Nope, consider what your code does if Energy = [0,1000]
It looks like you are making the common beginner mistake of using IF/ELSEIF/ELSE when you should be using indexing.
As we are doing the some simply calculation,our input for Energy(500), would only have 1 integer without involve matric and indexing.

请先登录,再进行评论。

 采纳的回答

Energy = app.EnergyUsageEditField;
Bill = Tariff(Energy);
You're passing the handle of an edit field in your app into Tariff. You most likely instead want to pass the value stored in that edit field into Tariff. If you're using a NumericEditField pass the Value property of the object Energy into Tariff.
Bill = Tariff(Energy.Value);
If you're using an EditField convert the Value property (which will be the text inside the edit field, not the number that text represents) of that object into a number.
Bill = Tariff(str2double(Energy.Value));

更多回答(1 个)

Why do you assume, that "the variable A is already defined", if Matlab displays clearly, that it is not?
What is your input Energy? If it is an array, the expression
if Energy <=200
is interpreted as:
if all(Energy(:) <= 200)
When none of the conditions is met for all elements of Energy, the variable A is not defined.
Use the debugger to examine what's going on: Set a breakpoint in the first line and step through your code line by line. You will see, that no code is executed, wich defines A.

3 个评论

My bad i didnt explain the question clearly,
input energy is from the design app,example,
% Button pushed function: calculateButton
function calculateButtonPushed(app, event)
Energy = app.EnergyUsageEditField;
Bill = Tariff(Energy);
app.BillRMEditField = Bill;
end
end
When i step through the code,the workspace at the rightside do have assign a value for A.but it doesnt work when i link up the function in the design app.
@Lee Yit Tung: then set DBSTOP IF ERROR and save the value of ENERGY in a mat file and upload it here by clicking on the paperclip button. Then we can help you to find out why your code does not work.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Instrument Control Toolbox 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by