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

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 Comments

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?
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

Sign in to comment.

Answers (5)

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.
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.

1 Comment

That logic for converting in to a column or row vector looks wrong to me.
Why not use
z = reshape(k.', 1, numel(k));

Sign in to comment.

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.

2 Comments

z is a vector with 4*p+4 elements. How can you store that into sys(p) ??
@Walter: Sorry, I overlooked the fact that z is a vector.
@Pramod: Ignore #1 and follow Walter's advice instead.

Sign in to comment.

@ 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 Comments

There is no built-in type for which sys(p) would give a vector, not unless p is itself a vector. Generally speaking, sys{p} could yield a vector (with appropriate initialization), and sys(p) could yield a 1 x 1 cell (a scalar) that _contained_ a vector (with appropriate initialization), but if sys is numeric and p is scalar, then sys(p) is scalar not a vector, and it is not possible to assign a vector of values in to a scalar location.
"string input" in what sense? If you refer to passing data between blocks, then Kaustabha has indicated that only numeric values can be passed. You could char() the numeric values to turn them in to strings once they were received.
But perhaps by "string input" you refer to loading data or reading it from a device or the like ?
@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..
Please show the current version of the code.

Sign in to comment.

@kaustubha This S Function takes Hexadecimal vector as input and gives its respective binary vector output..

6 Comments

No matter where the S function gets its data, if you want it to have binary output, then you need to have binary output (zeros and ones), not character output ('0' and '1' are characters, with numeric values 48 and 49 respectively.)
@walter you are right,yhe output of the S function, gives the character with 1's and zeroes, but these character's are in a form of vector, so that it makes the bit operations easier than other methods...
Using real 0 and 1 instead of '0' and '1' would be even easier for bit operations.
the bit operations I mean.... suppose.. ab - BIT- 10101011...
so in this I want bit 3 to 5, then it makes me easier to separate out the bits and take the necessary bits....
That's why the code is easier for such operations,...:)
Also the specified length for the following bits limit are not specified before the operation, so it is dependent on the preceding bit ...
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.

Sign in to comment.

Products

Tags

No tags entered yet.

Asked:

on 29 Apr 2011

Community Treasure Hunt

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

Start Hunting!