Problem loading variables to Simulink

I have a Simulink model that uses variables set in Matlab code. However it keeps giving me the same error.
I have this function
function y = tclabsim(t, x0, u, p)
% Ensure input sizes match
if length(t) ~= length(u)
error('Time vector t and input profile u must have the same length.');
end
% Extract parameters from p
U = p(1); % Heat transfer coefficient
alpha = p(2); % Ambient heat loss parameter
tau = p(3); % Time constant
% Initial conditions
x0_T = x0; % Initial temperature
% Ensure t and u are column vectors
t = t(:);
u = u(:);
% Create input matrices
inputMatrix = [t, u];
U_matrix = [0, U];
alpha_matrix = [0, alpha];
tau_matrix = [0, tau];
x0_T_matrix = [0, x0_T];
% Set simulation stop time
stopTime = t(end);
% Assign all required variables to base workspace
assignin('base', 'x0_T', x0_T_matrix);
assignin('base', 'U', U_matrix);
assignin('base', 'alpha', alpha_matrix);
assignin('base', 'tau', tau_matrix);
assignin('base', 'inputMatrix', inputMatrix);
% Run the Simulink model
simOut = sim('SingleHeaterTCLab_1.slx', 'SrcWorkspace', 'current', ...
'StopTime', num2str(stopTime));
% Extract the output from the simulation
y = simOut.Ts; % Sensor temperature output
end
And I call the function like this
% Time vector (0 to 1000 seconds with 1-second steps)
t = (0:999)'; % Make sure t is a column vector
% Initial temperature (20°C)
x0 = 20;
% Heater input (step response: 50% heater power after 100s)
u = zeros(size(t));
u(t >= 100) = 50;
% Parameters [U, alpha, tau]
p = [0.01, 0.01, 200]; % Example values for U, alpha, tau
% Simulate the system
y = tclabsim(t, x0, u, p);
However, every time I try to run it gives the same error.
I have tried to load them as simple double and I have also tried a matrix with t on the first column and the same value in every line of the second column. They also gave the same error.
The block in Simulink has these properties. And I have also tried to change the sample time to -1.
This is the entire simulink if it helps.
How can I fix it?
Thank you

 Accepted Answer

function y = tclabsim(t, x0, u, p)
%...
x0_T = x0; % Initial temperature
The size of x0 is uncertain at this point.
x0_T_matrix = [0, x0_T];
That would only work if x0_T is empty or scalar or a row vector. The result x0_T_matrix will be a scalar or a row vector.
assignin('base', 'x0_T', x0_T_matrix);
So that will be scalar or a row vector.
You then import it with a From Workspace block. From Workspace allows a few different datatypes, but when the data is numeric, it needs to be a 2D array. You are specifying a scalar or row vector instead of a 2D array.
If you are trying to import x0_T as a constant scalar, then import x0 as the initial value of a Constant block.

1 Comment

Thank you!
That made a lot of sense and it worked.

Sign in to comment.

More Answers (0)

Categories

Find more on Modeling in Help Center and File Exchange

Products

Release

R2023b

Asked:

on 11 Jan 2025

Commented:

on 11 Jan 2025

Community Treasure Hunt

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

Start Hunting!