Modeling a resistor using custom Simscape block and lookup tables using data from an excel sheet.

8 次查看(过去 30 天)
Hey, I am trying to model a resistor but whenever I try to read the specific columns from the excel sheet, Matlab gives an error 'Invalid use of non-parametric value 'Data'. All entities referenced in member declarations must be parameters'. I have specified the excel file in the same folder as the .ssc file. My execution steps are correct, there is only something wrong with my code. I am affixing the code below, can you pls rectify it. I have commented the error. I am also attaching the excel file 'Battery_Parameters.xlsx'. Pls fix any other errors if you feel so and give feedback if I'm doing anything wrong.
Code:
component Cell
nodes
p = foundation.electrical.electrical; %+:left
n = foundation.electrical.electrical; %-:right
end
variables
i = {0,'A'};
v = {0,'V'};
SOC = {0,'Pa'}; % Provided simply as a unit is required
Data = xlsread('Battery_Parameters.xlsx');
R_Discharge = {0,'Ohm'}; % resistance
end
parameters (Size=variable)
xd = Data(2:end,1) %Error
yd = Data(2:end,4) %Error
end
function setup
if R_Discharge<0
pm_error('simscape:GreaterThatOrEqualToZero','Resistance')
end
end
branches
i: p.i -> n.i;
end
equations
v == p.v - n.v;
R_Discharge == tablelookup(xd, yd, SOC, interpolation=linear, extrapolation=nearest);
end
end

回答(2 个)

Zeng
Zeng 2019-4-17
In my opnion, you should read the file in "Setup".

Pramil
Pramil 2024-10-17
Hi Pranav,
For loading external data in a SSC file you can use "MATLAB Declaration Functions". Here is the link for the following:
Here is the updated code that works in both MATLAB R2024a as well as R2018a:
component Cell
nodes
p = foundation.electrical.electrical; %+:left
n = foundation.electrical.electrical; %-:right
end
variables
i = {0,'A'};
v = {0,'V'};
SOC = {0,'Pa'};
R_Discharge = {0,'Ohm'}; % resistance
end
parameters (Access = private)
[xd,yd] = my_fcn();
end
branches
i: p.i -> n.i;
end
equations
assert(R_Discharge>=0);
v == p.v - n.v;
R_Discharge == tablelookup({xd 'Pa'}, {yd 'Ohm'}, SOC, interpolation=linear, extrapolation=nearest);
end
end
Where "my_fcn" is:
function [xd,yd] = my_fcn()
Data = xlsread('Battery_Parameters.xlsx');
xd = Data(2:end,1);
yd = Data(2:end,4);
end
Hope it helps.

类别

Help CenterFile Exchange 中查找有关 Choose and Parameterize Blocks 的更多信息

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by