MATLAB Answers

0

Read nx1 matrix from Matlab workspace into Simulink, but get the values one at a time

Asked by Emerson Butler on 3 Aug 2018
Latest activity Edited by Emerson Butler on 7 Aug 2018
Hi, I am trying to read an nx1 matrix from my workspace into my Simulink model one at a time. Ideally, I'd like to be able to use the discrete Delay blocks and view the outputs side by side with the non-delayed signal. Suppose my matrix is a column that counts from 1 to 10. I'd like to import that matrix in such a way that if I hooked up a scope, the scope would output something that looked like a step counter from 1 to 10 which stepped up once per sample time.
To try to be more clear: I have a matrix that contains data points. Each data point was captured at a fixed rate after the previous sample. This could be an example of my matrix:
1
2
3
4
5
6
7
8
9
10
So I'd like to view that in a scope in Simulink as I described above. If those were my data points, I would want the scope to cycle through each entry in the matrix as the simulation progresses. My actual data points will be doubles that are not evenly incrementing like that, they are data points captured from a sinusoidal type wave. Additionally, I'd like to run a wire from that matrix into some discrete delay blocks so that I could compare the value of the matrix during the present sample to some delayed sample. So, if I have a discrete delay block set to delay the signal by 2 samples, I could compare my signal in its current sample to what it was 2 samples ago, so that if I hooked both current and delayed signals up to a scope I could view the data side by side. I do know the actual sample start time and increment time, so if I have to create a new matrix combining the data with a time stamp that would be a possibility, but I would like to avoid that if possible.
I have tried using constant blocks, but they just display the entire matrix all at once. Some other data import blocks require a separate time column, which I would like to avoid since my system doesn't rely on time in any other way due to the constancy of the timing. I am just looking for a way to import my matrix in such a way that each row can be considered a sample.
I hope this has been a clear enough explanation. I already have an enormous model built that will entirely rely on being able to read in data in the way that I have described, so I would like to have my data import method fit the parameters I have described. I realize I should have figured this out first, but that's just how it goes I suppose. Thanks in advance for any help or guidance.

  0 Comments

Sign in to comment.

Products


Release

R2017a

1 Answer

Answer by TAB
on 6 Aug 2018
 Accepted Answer

Use an index counter and extract the one array elements at every sample. It can be done in both Simulink and Stateflow.
Below is just idea using stateflow:
/* Initialization */
idxCtr = 0;
/* During */
out = myarray[idxCtr];
idxCtr = idxCtr+1;
if(idxCtr>9)
{
idxCtr = 0; /* Depend on your logic you can freeze/reset the counter here.*/
}
You can send "out" to scope or any other block for further process.

  2 Comments

Fantastic! Thank you. I ended up assigning my matrix to a constant block in Simulink. I used that in combination with a Free-Running Counter block as the index to a Variable Selector Block. I'm still working on getting it to display the full matrix since it's only displaying the first half (roughly) of the data points, but I can see the first half of the sinusoidal-ish waveform if I plug the output of the Variable Selector block into a scope. I think I just need to mess around with the block parameters to view the full matrix.
I changed the default "-1" timing parameters to 1 in both the constant block and the Free-Running Counter block, and that solved the issue. Thanks!

Sign in to comment.