Unwanted "ans" value generated (comm.RSEncoder)

1 view (last 30 days)
Das Siddharth
Das Siddharth on 9 Nov 2021
I am encountering an unusual issue. In the following script if I don't make it a function everything works fine and I am getting a perfectly decoded message. But when I make it a function the decoded message and the "ans" bits aren't the same. All the values are assigned properly. I am not sure where the problem is arising.
Thank you in advance !
The corresponding output as an example is here:
function [sent,received] = ans_mf()
clear all;
K=3; %%% variable
N=7; %%% variable
primPolyDegree = 3; %%% variable
% Create RS(7,3) encoder/decoder object
rsEnc = comm.RSEncoder(...
'BitInput', true,...
'CodewordLength', N,...
'MessageLength', K);
rsEnc.PrimitivePolynomialSource = 'Property';
rsEnc.PrimitivePolynomial = de2bi(primpoly(primPolyDegree,'nodisplay'),'left-msb');
rsDec = comm.RSDecoder(...
'BitInput', true,...
'CodewordLength', N,...
'MessageLength', K);
rsDec.PrimitivePolynomialSource = 'Property';
rsDec.PrimitivePolynomial = de2bi(primpoly(primPolyDegree,'nodisplay'),'left-msb');
bin_mess= randi([0 1],1,9);
bin_mess_transp = bin_mess' ;
message_in_bin_rsenc = rsEnc(bin_mess_transp);
disp('encoded messages:');
if message_in_bin_rsenc(3,1) == 1
message_in_bin_rsenc(3,1) = zeros;
message_in_bin_rsenc(3,1) = 1;
disp('erroneous message:');
message_in_bin_rsdec = rsDec(message_in_bin_rsenc);
disp('decoded messages:');
sent = message_in_bin_rsenc';
received = message_in_bin_rsdec' ;

Answers (1)

Ashutosh Singh Baghel
Ashutosh Singh Baghel on 17 Nov 2021
Hi Siddharth,
I understand you wish to know why there are different outputs when executing the script, usually and in function.
The resolution can be understood as follows -
  1. When the script is executed, the workspace used is usually the base workspace, and all variables are listed here only. Calling 'clear' will clear this base workspace. Hence, the results are expected here.
  2. When the script is made as a function file, the workspace is now specific to that particular function and not the base workspace. Also, calling 'clear' inside the function will clear the function workspace, not the base workspace. This is not an issue but just an output of different workspaces in the command window.
While calling the function script from a command window, without specifying any output arguments, MATLAB automatically assigns a variable named 'ans' to the 'varargout.' Hence, you see the value stored in the 'sent' variable as stated in your function definition.
Refer to the following MATLAB Documentation page links to 'Base and Function Workspaces' and 'Ignore Function Outputs' for further information.

Community Treasure Hunt

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

Start Hunting!