Matlab c embedded coder code

468 Views Asked by At
  b_k = 1;
  while (b_k <= iv0[1]) {
    h = vplus_data[0];
    u1 = vmax->data[(int)((1.0 + (double)k) + 1.0) - 1];
    if ((h <= u1) || rtIsNaN(u1)) {
      minval_data_idx_0 = h;
    } else {
      minval_data_idx_0 = u1;
    }

    b_k = 2;
  }

  b_k = 1;
  while (b_k <= iv0[1]) {
    h = vmin->data[(int)((1.0 + (double)k) + 1.0) - 1];
    if ((h >= minval_data_idx_0) || rtIsNaN(minval_data_idx_0)) {
    } else {
      h = minval_data_idx_0;
    }

    vplus_data[0] = h;
    b_k = 2;
  }

this code is compared to min function to get the minimum value for h or u1, can anyone tell me why matlab generate such syntax? why is the while loop, although I dont see any changes inside the while block!

matlab code

v(k+1) = max(vmin(k+1), min(vplus, vmax(k+1)));

notice there are two loops for max min function

1

There are 1 best solutions below

0
On

I can't explain why the generated code ends up like that, but it must have something to do with how you wrote your Matlab code. It looks strange, but if it works then it probably doesn't matter.

If you're curious about the generator, start from something very simple and watch how the generated code changes as your code gets more complex. Try variations like these:

z = min(x, y);

z = max(w, min(x, y));

for i = 1:length(v)
    z(i) = max(w, min(v(i), y));
end

Keep on modifying the test code a bit at a time to make it like the code that prompted this question and maybe you'll discover exactly what triggers the result you're seeing.