matlabsimulink# How to use subsystem output as its input in the next iteration in simulink

I am trying to create SHA256 hash function in Simulink (2015a) while abstaining as much as possible from using user-defined functions - using them is not the answer I am looking for.

I have tried using Delay block, but it also delays my a-h variables from initialization (it means, for example, my T1 will be 0), so the problem lies there probably

I am new to this kind of thing so I am sorry if it's a question that has already been answered I simply don't know what to google to get my answer since English is not my first language, anyhow here is my scheme:

For loop (but with placeholder const 1)

First iteration is giving me the correct output

It should represent this MATLAB code:

```
% H = 1x8 const
% constK = 1x64 const
a = H(1);
b = H(2);
c = H(3);
d = H(4);
e = H(5);
f = H(6);
g = H(7);
h = H(8);
% Calc H1 subsystem in Simulink
for t = 1:64
K = constK(t);
temp1 = addMsgSch(h, cSigma1(e), choice(e, f, g), K, W(t));
temp2 = addMsgSch(cSigma0(a), majority(a, b, c));
h = g;
g = f;
f = e;
e = addMsgSch(d, temp1);
d = c;
c = b;
b = a;
a = addMsgSch(temp1, temp2);
end
% addMsgSch(a, b... z) = a + b + ... + z;
```

So my question is: *How can I (re)start the loop with newly assigned variables?*

Solution

To preface this: I don't have 2015a (and so can't readily test this), and am not specifically familiar with the ins and outs of SHA256.

The `Delay`

block's default behavior is to initialize its output signal with all zeroes, but that is not its only available behavior. The initial condition can be set on the mask, or (at least in recent versions) there is a check box on the mask that will "magically" make an additional input port appear:

x0 — Initial condition scalar | vector

Initial condition specified as inherited from an input port. Enabled when you select the Initial Condition: Source parameter as Input port.

(A distressing number of built-in Simulink blocks have similar "polymorphic" abilities to change the input or output ports)

After enabling this additional port, you can route the H signal into it (and presumably H1 into the "main" input of the `Delay`

block).

For your awareness, a number of Simulink blocks have some mechanism to set initial conditions, usually with a mask parameter. There is also the related `IC`

block, which can set initial conditions on a signal based on a mask parameter.

FWIW, in my experience implementing something like this in "pure" Simulink doesn't really play to Simulink's strengths. Simulink is great for managing signal (and thus information) routing, and its dynamics engine is quite powerful. But for doing "simple" math, the pile-up of blocks is often less clear than using a Matlab Function block. The chunk of code inside your m-code `for`

loop is much more straightforward to me than the equivalent Simulink (and your Simulink is laid out in a pretty reasonable way).

Also FYI, the `Subsystem`

block that you are using to split H into a...h could probably be replaced with a `Demux`

.

And a further FYI, it appears to me that your use of `addMsgSch(a, b... z)`

could be replaced with `sum([a, b... z])`

or just regular addition, either of which would improve readability.

- how to reshape a cell array to matrix in desired size
- Using Mat_CreateVer for MATLAB versions higher than 7.3
- Making a numpy ndarray matrix symmetric
- Matlab 2021 and2023 cross compatability
- How to plot figures at the end of the for loop MATLAB?
- Variable transfer from App Designer to general .m script file
- Anomalous behavior of the Simulink signal - Simscape
- What is the fastest method to searsch for a part of a matrix inside of a large matrix?
- Can I Prevent Git from Using XTerm Escape Sequences?
- Use VPA to evaluate tiny probabilities
- Generate Array of Random Numbers with Specified Standard Deviation and Mean
- Matlab update plot with multiple data lines/curves
- Matlab vectorize for loop (grid depends on loop index)
- How to use a flag variable to break all the for-loops in spmd statement?
- How to produce the indexes from pdist2 function in Octave?
- Calculation of sensitivity and specificity from confusion matrix
- How do I read comma separated values from a .txt file in MATLAB using textscan()?
- How do you code Garson's algorithm in matlab to find the relative importance of parameters when training a neural network model?
- add data label to a grouped bar chart in matlab
- Using 1D convolutional with same shape so it will work with FFT?
- super fast median of matrix in OpenCV (as fast as MATLAB)
- How to create a struct or type from a dictionary in Julia?
- MATLAB Delay an Input Signal
- Is there an Octave equivalent of Matlab's `contains` function?
- Matlab define callback function for mouse click on a biograph
- getting a return value from a function in MATLAB
- How to convert nifti file to AFNI file?
- load .mat file, but not on the workspace
- Fast plane fitting to many points
- Cubic spline interpolation vs polynomial interpolation