Can anyone please tell me how to design a block for converting a hex to Binary

2 次查看(过去 30 天)
Can any one suggest me to how to make a block for converting a hex code to binary code (vector)...
I have done programming using in M file, that takes inputs directly from the workspace, is there anything I can do to make this S function independent from Workspace....
Kindly let me know as soon as possible..
function [sys,x0,str,ts] = input_abc(t,x,u,flag)
% Dispatch the flag. The switch function controls the calls to
% S-function routines at each simulation stage.
switch flag,
case 0
[sys,x0,str,ts] = mdlInitializeSizes; % Initialization
case 3
sys = mdlOutputs(t,x,u); % Calculate outputs
case { 1, 2, 4, 9 }
sys = []; % Unused flags
otherwise
error(['Unhandled flag = ',num2str(flag)]); % Error handling
end;
% End of function timestwo.
%==============================================================
% Function mdlInitializeSizes initializes the states, sample
% times, state ordering strings (str), and sizes structure.
%==============================================================
function [sys,x0,str,ts] = mdlInitializeSizes
% Call function simsizes to create the sizes structure.
k = input('Specify the length of the String');
sizes = 4;
% l = input('Specify the length of the String');
% Load the sizes structure with the initialization information.
sizes.NumContStates= 0;
sizes.NumDiscStates= 0;
sizes.NumOutputs = 4*k %input('Specify the no. outputs');
sizes.NumInputs= 1;
sizes.DirFeedthrough=1;
sizes.NumSampleTimes=1;
% Load the sys vector with the sizes information.
sys = simsizes(sizes);
%
x0 = []; % No continuous states
%
str = []; % No state ordering
%
ts = [-1 0]; % Inherited sample time
% End of mdlInitializeSizes.
%==============================================================
% Function mdlOutputs performs the calculations.
%==============================================================
function sys = mdlOutputs(t,x,u)
l = input('Specify the length of the String');
for p = 1:1:l
y(1,p:l) = [input('Give the Hex Value','S')]
end
for p = 1:1:l
if y(1,p) == 'A'
a = [1 0 1 0]
for h = 1:1:4
k(p,h) = a(1,h)
end
elseif y(1,p) == 'B'
b = [1 0 1 1]
for h = 1:1:4
k(p,h) = b(1,h)
end
elseif y(1,p) == 'C'
c = [1 1 0 0]
for h = 1:1:4
k(p,h) = c(1,h)
end
elseif y(1,p) == 'D'
d = [1 1 0 1]
for h = 1:1:4
k(p,h) = d(1,h)
end
elseif y(p) == 'E'
e = [1 1 1 0]
for h = 1:1:4
k(p,h) = e(1,h)
end
elseif y(p) == 'F'
f = [1 1 1 1]
for h = 1:1:4
k(p,h) = f(1,h)
end
elseif y(p) == '9'
nine = y(p)
nine = [1 0 0 1]
for h = 1:1:4
k(p,h) = nine(1,h)
end
elseif y(p) == '8'
eight = y(p)
eight = [1 0 0 0]
for h = 1:1:4
k(p,h) = eight(1,h)
end
elseif y(p) == '7'
seven = y(p)
seven = [0 1 1 1]
for h = 1:1:4
k(p,h) = seven(1,h)
end
elseif y(p) == '6'
six = y(p)
six = [0 1 1 0]
for h = 1:1:4
k(p,h) = six(1,h)
end
elseif y(p) == '5'
five = y(p)
five = [0 1 0 1]
for h = 1:1:4
k(p,h) = five(1,h)
end
elseif y(p) == '4'
four = y(p)
four = [0 1 0 0]
for h = 1:1:4
k(p,h) = four(1,h)
end
elseif y(p) == '3'
three = y(p)
three = [0 0 1 1]
for h = 1:1:4
k(p,h) = three(1,h)
end
elseif y(p) == '2'
two = y(p)
two = [0 0 1 0]
for h = 1:1:4
k(p,h) = two(1,h)
end
elseif y(p) == '1'
one = y(p)
one = [0 0 0 1]
for h = 1:1:4
k(p,h) = one(1,h)
end
elseif y(p) == '0'
zero = y(p)
zero = [0 0 0 0]
for h = 1:1:4
k(p,h) = zero(1,h)
end
end
end
for p = 1:1:l-1
z = [k(p,1:4*p),k(p+1,1:4)]
p = p+1
k(p,1:4*p) = z
sys = z
end
  2 个评论
Jarrod Rivituso
Jarrod Rivituso 2011-4-29
Could you explain at a higher level what you are trying to accomplish? It is odd for me to see calls to the "input" function within an S-function.
In my mind, an S-function is something that Simulink will call lots of time during a simulation, and it would be tedious for a user to keep having to enter things every time step. Or am I missing something?
Pramod Kumar
Pramod Kumar 2011-5-2
Actually I am trying to make a output vector of binary bits (sys(p)), for which i have to give hexadecimal input... to the block

请先登录,再进行评论。

回答(5 个)

Walter Roberson
Walter Roberson 2011-4-29
I don't know have any idea how to get the input value in block style, but why are you using such inefficient code??
B = [0 0 0 0;0 0 0 1; 0 0 1 0; 0 0 1 1; 0 1 0 0; 0 1 0 1; 0 1 1 0; 0 1 1 1; 1 0 0 0; 1 0 0 1; 1 0 1 0; 1 0 1 1; 1 1 0 0; 1 1 0 1; 1 1 1 0; 1 1 1 1];
k = zeros(l,4);
k(y <= '9', 1:4) = B(y(y <= '9')-'0'+1, 1:4);
k(y >= 'A', 1:4) = B(y(y >= 'A')-'A'+10+1, 1:4);
I do not follow what you are doing in the final loop, especially as you keep overwriting sys.

Pramod Kumar
Pramod Kumar 2011-4-29
Thank you very much for your reply... I am just a beginner to... So i implemented as I was getting though with the logic...... The last loop, is converting the whole K matrix into a single column or row matrix vector, so that I can get outputs from the S function.
Also sys is mentioned because it specifies the output of the block..... in its first call it takes it's values that specifies the no. of outputs the block is giving... then at the end it was assigned a Single column/row matrix to it, so that i can give me an output.

Kaustubha Govind
Kaustubha Govind 2011-4-29
I see a few issues with this code:
1. As Walter pointed out, you are over-writing 'sys' in the for-loop. I think what you really want is:
for p = 1:1:l-1
z = [k(p,1:4*p),k(p+1,1:4)]
p = p+1
k(p,1:4*p) = z
sys(p) = z
end
2. Your input seems to be characters - Simulink only supports numeric signals. It's not clear what this S-function achieves, but you may want to convert this to using corresponding ASCII values instead (or however you can implement this with numeric signals)
3. It seems that the width of your input and output is determined by 'l' which changes during simulation. You need to use a variable-size signal to achieve this (not sure how you do this in your current model). AFAIK, Level-1 MATLAB S-functions (which is what your code implements) do not support variable-size signals. Note that Level-1 S-functions have also been deprecated and are only supported for backward-compatibility. I would recommend converting this to a Level-2 MATLAB S-function if possible. Once you have done that look at this example for using variable-size signals with your S-function.

Pramod Kumar
Pramod Kumar 2011-5-2
@ walter sys(p)... is column/row matrix vector, it gives output in a form of a single vetor....
does anyone knows how to give string input using simulink blocks ??
  4 个评论
Pramod Kumar
Pramod Kumar 2011-5-2
@walter..
From the code....sys(p) stores the value of last column of the Z matrix,as the p is define in a for loop.... thus sys(p) will give output in vector form..
I have implemented this code, this code works fine... you can check with it..

请先登录,再进行评论。


Pramod Kumar
Pramod Kumar 2011-5-2
@kaustubha This S Function takes Hexadecimal vector as input and gives its respective binary vector output..
  6 个评论
Pramod Kumar
Pramod Kumar 2011-5-2
Also the specified length for the following bits limit are not specified before the operation, so it is dependent on the preceding bit ...
Walter Roberson
Walter Roberson 2011-5-2
Taking elements 3 to 5 of a vector of numeric values is the same work as taking elements 3 to 5 of a character string. However, it is far easier to do things like ~(a | b) for numeric values than for character strings.

请先登录,再进行评论。

类别

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

标签

尚未输入任何标签。

产品

Community Treasure Hunt

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

Start Hunting!

Translated by