Why does my Nx2 matrix turn into a 1x1 when I pass it into a MATLAB Function block

6 次查看(过去 30 天)
So I want to change the values in a reservoir dynamically per second, but since I cannot have input ports I tried to change it via changing the variable value the parameter is referencing. So I tried indexing my Nx2 matrix to change the value as time passes by the second, but when I pass the 'From workspace' block into the MATLAB Function block, a constant error of index error appears, telling me 'Index expression out of bounds. Attempted to access element 2. The valid range is 1-1.'
The index used was [idx, 2], idx being the time used for indexing, since my data has 'time in seconds' and 'temperature'.
Is there a way to fix this? Or is my approach wrong to begin with? Thanks in advance!
  1 个评论
Kai Men
Kai Men 2024-1-23
For reference, I am trying to change reservoir_temperature and reservoir_RH which are referencing variables T_env and RH_env using data collected and interpolated to have data every second.

请先登录,再进行评论。

回答(2 个)

Aquatris
Aquatris 2024-1-23
Your matrix you are getting from workspace needs to be Nx3 in your case, where the 1st colum is time, 2nd and 3rd column are your actual data.
Or instead of using a matrix variable, use a structure variable as described in the block itself.
  2 个评论
Kai Men
Kai Men 2024-1-23
编辑:Kai Men 2024-1-23
Hi, just for clarification, I have 2 Nx2 matrix, corresponding to time&temperature, and time&RH, does this mean I should combine it all into a Nx3 matrix with time, temp and RH?
Aquatris
Aquatris 2024-1-23
编辑:Aquatris 2024-1-23
Correct, x = [time temperature RH]; should do the trick.
Make sure the time vectors of your 2 Nx2 matrices are identical before combining them into a Nx3 matrix. Otherwise you should either use 2 'from workspace' blocks and combine the data via a 'mux' or you should interpolate to achieve same time vectors for both before combining them in order to have sensible results from your simulation.

请先登录,再进行评论。


Walter Roberson
Walter Roberson 2024-1-23
When you use From Workspace, then the first column of the data is treated as the time, and the remaining columns of the data are treated as values over time.
So when you have your 2 x 2 matrix, the first column is being treated as the time, and the second column is being treated as scalars at the times given by the first column.

类别

Help CenterFile Exchange 中查找有关 Sources 的更多信息

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by