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.
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!
回答(2 个)
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1597396/image.png)
2 个评论
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
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Sources 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!