Converting 1D array into a constant (taking the last value of the array)

I have a embbed matlab function(EMB1) in front that does the convergence so the last value of the array is the converged value. Currently, the rest of the model will take the non-converged as it is at their time step. What I wanted to do is to take the last value (i.e. the last time step) of the array and apply it to the rest of the model(at all time step).
I have tried transposing the array into row vector(?)(i.e. into a single row and 400 column) then using another embbed matlab function(EMB2) to select the last value to continue but somehow the transpose block is not working as out.simout does not show it on the row vector and subsequently got hit with the 'index expression out of bounds' error. Below shows the matlab script for EMB2, where 400 is the last value in the array:
function y = fcn(u)
u=u(400);
y=u;
end
From what I have trial and error, the term 400 in the bracket is for the 400th column and not the 400th row which is why i tried (and failed) to transpose the 1D array. Please advise on the next step/suitable block to use. I have been stuck on this for 2 days now.

Answers (1)

Not sure why
function y = fcn(u)
y = u(end);
doesn't work. It seems to work for me, for both vectors and multidimensional arrays.
What are you passing in for u (number of rows and columns), and which element do you want returned?

5 Comments

I tried your matlab script but it didn't work. The matlab function does not seems to do anything, i.e. input data = output data. There are 400 rows (stop time of 400) and 1 column. I wanted the element in 400th row to be returned for use in all time step for the subsequent model
It does work. If your input data is a column vector of 400 rows, fcn(u) will return u(400) which is the last value. Just try it. Here is a complete script demo:
u = rand(400, 1)
y = fcn(u);
fprintf('u(400) = %f, y = %f\n', u(400), y);
function y = fcn(u)
y = u(end);
end
In the command window, you'll see the proof:
u =
0.33536
0.67973
...
0.49036
0.853
0.87393
0.27029
0.20846
u(400) = 0.208461, y = 0.208461
See, it returns the last value. If you still think it doesn't work, attach your whole m-file with the paper clip icon.
Thanks for the prompt reply!
Your script does work when it is in matlab only. However, when I substitute 'u' to the as the input of the embbed matlab function (EMF) (which is the output of of another EMF). I have also tried putting y=u(end) at the end of the first EMF but out.simout is still not showing the last value only
Sorry, but I don't have the MATLAB Coder Toolbox, or whatever is required to make embedded (in silicon) modules. I also don't know what simout is. If that's Simulink, I don't have Simulink.
Ahh I see. Yes this is simulink. Thank you for your help nonetheless

Sign in to comment.

Categories

Products

Release

R2019b

Asked:

on 7 Mar 2020

Commented:

on 8 Mar 2020

Community Treasure Hunt

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

Start Hunting!