0

I'm currently trying to learn some basis for a bigger project that will make a massive use of simulink. Right now, I would like to code my own simulink block whith a feedback. It means that one of the inputs is also the output (with a 'memory' block between them to ensure everything goes right!).

My code looks like

function out = func(cmd,in)

if in == 0 && cmd == 1
    out = 1;
elseif in == 1 && cmd == 0
    out = 0;
else
    disp('error')
end

As I said, 'in' is linked to 'out'. Unfortunatly, it is required to set an initial value for out otherwise I get some errors. Of course I can't do it in the code like that :

out = 0;

In that case, the value 'out' is set to 0 at each time step.

Have you any advice to do it ? I've read that S-functions and flag could be used, but I have no idea how it works.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Ezor
  • 135
  • 2
  • 18

2 Answers2

1

Your function gets called during model initialization (t=0), and the value of out will be calculated based on the value of cmd and in value at t=0.

Hence you need to make sure cmd and in are initialized correctly you shouldn't be setting a value explicitly for out.

If you really need to (which you won't) then the simplest thing to do is use an Initial Condition block after this block.

Note that the above is only applicable to a block that has no states, as with your example. For your bigger project, you may have custom written blocks with states, in which case the approach to setting initial conditions for the states is different depending on whether you are using a MATLAB Function block or an S-Function.

Finally, note that if you want an error to be thrown then throw an error in the usual MATLAB way. Using disp as you are doing doesn't stop the simulation, but you haven't set a value for out, which is bad coding.

Phil Goddard
  • 10,571
  • 1
  • 16
  • 28
  • Thank you for your help. First, the `disp` command is just here to explain you what I'm going to do. This is not my real code. That's why there are weird things in it ! I have already tried to use `Initial condition` block just after the `Matlab function` one but it hasn't changed anything. Maybe I am using it in the wrong way. I'll look into it. I also did what you've told me. I set `cmd =0` and `in=0` in the workspace before lunching the simulation but I got the same error message _Output argument 'out' is not assigned on some execution paths_ Anyway thanks again for your answer – Ezor Oct 28 '16 at 07:22
  • Since I will probably use `if t=0` to set the initial condition, do you know how I can get the time in my function without adding a clock and a new input? I don't want to add extra input for all my future blocks ! – Ezor Oct 28 '16 at 08:48
  • You do not want time as an input to your block. There is no need for it. Ensure that the output is assigned for all possible paths through your code, which is not happening for the code you've shown, and is why you get the error you give in your comment (which should have been included in the question). – Phil Goddard Oct 28 '16 at 15:59
0

Well, I think that I've solve that issue. I put it here, I could help someone else

  • by adding a clock and initialize out with an if statement if time <=0 out = ... However, it requires to add an extra input, which is not very convinient. Maybe someone could tell me how to solve that.
  • do the if properly and terminate it by else out = in;

I belive that was the main problem here. I've also placed my matlab-function block in a subsystem with a mask that initializes in and cmd.

Thanks again for your help, it helped a lot. However, my problem is still unsolved since the statement if t<0 doesn't work for some reason.

Ezor
  • 135
  • 2
  • 18
  • You'll never execute a `t<0` path because `t` is never less than 0. Adding the extra time input is the wrong way to use Simulink. As per my answer, for a block that does not contain states, the output is purely a function of the input and it's value (at t=0 or otherwise) and it's the inputs that need to be initialized correctly to get the correct output. – Phil Goddard Oct 28 '16 at 15:55