how to convert file.m into block in Simulink ?

1 次查看(过去 30 天)
hi,
I wrote a function that produce 40 binary bits vector according to another 20 bits vector. In Matlab it works ok.
I want to convert it into a blockqmodel in Simulink.
Anybody knows how to do it ?
for example:
function gate_h = gate_array(I_H, indx_h)
no_cells = 20;
if(I_H > 0)
TMP_reg = [zeros(1, no_cells-indx_h), ones(1, indx_h)];
else
TMP_reg = [ones(1, indx_h), zeros(1, no_cells-indx_h)];
end
bitpatterns = {[0 1], [1 0]};
gate_h = cell2mat(bitpatterns(TMP_reg + 1));
Thanks, Henry

采纳的回答

Walter Roberson
Walter Roberson 2016-3-4
Try using the following as a MATLAB Function block
function gate_h = gate_array(I_H, indx_h)
no_cells = 20;
gate_h = zeros(1, no_cells * 2);
gate_h(1:2:indx_h*2) = 1;
gate_h(indx_h*2+1:2:no_cells*2-1) = 1;
if I_H > 0
gate_h = 1 - gate_h;
end
  5 个评论
Henry Buck
Henry Buck 2016-3-4
编辑:Walter Roberson 2016-3-4
Hi,
I made a simple change and it seems to be works ok.
%%%%%%%%%%%%
function gate_h = gate_array_h6(I_H, indx_h)
no_cells = 20;
gate_h = zeros(1, no_cells * 2);
gate_h(1:2:indx_h*2) = 0;
gate_h(indx_h*2+1:2:no_cells*2-1) = 0;
if I_H > 0
gate_h(2:2:(no_cells-indx_h)*2) = 1;
gate_h((no_cells-indx_h)*2+1:2:no_cells*2-1) = 1;
else
gate_h(1:2:indx_h*2-1) = 1;
gate_h((indx_h+1)*2:2:no_cells*2) = 1;
end
%%%%%%%%%%%%
No E R R O R at all ....:-)).
Thanks a lot,
Henry
Henry Buck
Henry Buck 2016-3-4
Hi,
For changing direction of the gate_h bits vector to complement gate_l bit vector, what do I need to change ? for example: in one block I got gate_h= [0 1 0 1 0 1 0 1 1 0 1 0]. and in anothe block I want to get gate_l = [1 0 1 0 1 0 1 0 0 1 0 1].
its like gate_l is complement of gate_h

请先登录,再进行评论。

更多回答(1 个)

Rick Rosson
Rick Rosson 2016-3-4
Have you tried using the MATLAB Function block? It's in the User Defined Functions sub-library in base Simulink.
  2 个评论
Henry Buck
Henry Buck 2016-3-4
Yes I did, But when I add that code into MATLAB Function code, after running I got lots of error relate to the code above.
I dont know how solve it.
Thanks, Henry
Henry Buck
Henry Buck 2016-3-4
Hi, I made a simple change and it seems to be works ok.
%%%%%%%%%%%% function gate_h = gate_array_h6(I_H, indx_h)
no_cells = 20; gate_h = zeros(1, no_cells * 2); gate_h(1:2:indx_h*2) = 0; gate_h(indx_h*2+1:2:no_cells*2-1) = 0;
if I_H > 0 gate_h(2:2:(no_cells-indx_h)*2) = 1; gate_h((no_cells-indx_h)*2+1:2:no_cells*2-1) = 1; else gate_h(1:2:indx_h*2-1) = 1; gate_h((indx_h+1)*2:2:no_cells*2) = 1; end %%%%%%%%%%%%
No E R R O R at all ....:-)).
Thanks a lot, Henry

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Simulink Functions 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by