Can anyone please tell me how to design a block for converting a hex to Binary
1 view (last 30 days)
Show older comments
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
Jarrod Rivituso
on 29 Apr 2011
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?
Answers (5)
Walter Roberson
on 29 Apr 2011
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.
0 Comments
Pramod Kumar
on 29 Apr 2011
1 Comment
Walter Roberson
on 29 Apr 2011
That logic for converting in to a column or row vector looks wrong to me.
Why not use
z = reshape(k.', 1, numel(k));
Kaustubha Govind
on 29 Apr 2011
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
Walter Roberson
on 29 Apr 2011
z is a vector with 4*p+4 elements. How can you store that into sys(p) ??
Kaustubha Govind
on 29 Apr 2011
@Walter: Sorry, I overlooked the fact that z is a vector.
@Pramod: Ignore #1 and follow Walter's advice instead.
Pramod Kumar
on 2 May 2011
6 Comments
Walter Roberson
on 2 May 2011
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.
See Also
Categories
Find more on String in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!