Importing data into a "Matlab function" simulink block and interpolating values

Hello everyone,
My problem is quite simple, but for several hours now I've been stuck with error messages that I can't correct.
I'm trying to create a simulink block which takes as input a continuous "speed" signal, a continuous "load" signal and an Excel table which references a temperature as a function of "speed" (1st line of my Excel, 61 values from 0 to 15m/s) and "load" (1st column, 61 values from 0 to 1000kg in constant steps). So I'm trying to write a script/command in a "Matlab function" simulink block that takes the input signals into account, and then interpolates the data to obtain a resulting temperature. Example: at a given time, speed = 2.12m/s and load = 628kg, which gives me for output an interpolated temperature in my table.
A first basic code worked on Matlab (without using simulink or else) taking into account fixed input values. Here it is below:
filename = 'Hysteresis Coefficient_Matlab.xlsx'; % Name of Excel file
sheet = 1;
data = xlsread(filename, sheet);
% Split the data into load, speed and temperature
load = data(2:62, 1); % First column
speed = data(1, 2:62); % First raw
temperature = data(2:62, 2:62);
% Define the input values
load_input = 522; % [500 kg]
speed_input = 5.1; % [m/s]
% Use of the interp2 fonction
temperature_researched = interp2(speed, load, temperature, speed_input, load_input);
% Display the researched temperature
fprintf('For a charge of %.1f kg and a speed of %.1f m/s, the interpolated temperature is %.2f\n', load_input, speed_input, temperature_researched);
Unfortunately, as soon as I want to do the same thing in a "Matlab function" simulink block so that all the simulation is done only on simulink, nothing works and I always get an error message like this:
An error occurred while running the simulation and the simulation was terminated
Caused by:
load = data(2:62, 1); % First column
I'm open to all your suggestions and I hope you'll be able to resolve the situation.
Thank you in advance
Thibault

2 件のコメント

Jon
Jon 2023 年 10 月 26 日
Please attach your .xlsx files, your Simulink files and any .m files that you make use of so that we can reproduce your problem
Jan Soller
Jan Soller 2023 年 10 月 26 日
Hello Jon, I also uploaded the concerned files. Hope it will be enough to run and test the simulation!

サインインしてコメントする。

 採用された回答

Jon
Jon 2023 年 10 月 26 日

0 投票

The problem is that the block that provides "data" is a from workspace block rather than a constant. You should replace it with a constant and assign the constant's value to the workspace variable you call data

4 件のコメント

Jon
Jon 2023 年 10 月 26 日
There is also a 2-D Lookup Table Block that perhaps you could make use of instead of using your own MATLAB fcn. It is found in the library browser under Simulink>Lookup Tables
Jon
Jon 2023 年 10 月 27 日
Did you get a chance to replace the from workspace block with a constant? Did this solve your problem?
Jan Soller
Jan Soller 2023 年 10 月 30 日
Hello Jon,
Sorry for this late answer, yes I finally got my results by replacing the from workspace with the constant block. I was convinced that I had already tried this solution at the beginning but apparently I maybe had trouble linking to my Data table.
Thanks a lot for your help!
Jon
Jon 2023 年 10 月 30 日
Glad you got it working, good luck with your project

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

製品

リリース

R2022b

質問済み:

2023 年 10 月 26 日

コメント済み:

Jon
2023 年 10 月 30 日

Community Treasure Hunt

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

Start Hunting!

Translated by