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 Kommentare

"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.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

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));

Weitere Antworten (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 Kommentare

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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Instrument Control Toolbox finden Sie in Hilfe-Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by