- 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.
- 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.
Unwanted "ans" value generated (comm.RSEncoder)
7 次查看(过去 30 天)
显示 更早的评论
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: ![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/795549/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/795549/image.png)
function [sent,received] = ans_mf()
clear all;
clc;
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:');
disp(message_in_bin_rsenc');
if message_in_bin_rsenc(3,1) == 1
message_in_bin_rsenc(3,1) = zeros;
else
message_in_bin_rsenc(3,1) = 1;
end
disp('erroneous message:');
disp(message_in_bin_rsenc');
message_in_bin_rsdec = rsDec(message_in_bin_rsenc);
disp('decoded messages:');
disp(message_in_bin_rsdec');
sent = message_in_bin_rsenc';
received = message_in_bin_rsdec' ;
disp('sent');
disp(sent);
disp('received:');
disp(received);
end
0 个评论
回答(1 个)
Ashutosh Singh Baghel
2021-11-17
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 -
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!