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 views (last 30 days)
Show older comments
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!
Answers (2)
Aquatris
on 23 Jan 2024
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 Comments
Aquatris
on 23 Jan 2024
Edited: Aquatris
on 23 Jan 2024
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
on 23 Jan 2024
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 Comments
See Also
Categories
Find more on Sources in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!