Question

Actually I am coding a Matlab simulation where the AnchorID and SourceID will report to eachother. For example if I take an anchor 30 and source 50 it will collect all the agc values between these anchor and source and calculate rssi_dB and display them.Below mentioned is an example of anchor 30 and source id 50

Note: list of anchors ID's and source ID's are same. for e.g. 30 50 55 58 . These ID are both same for anchor and source.

function A30(BlinkSet)
for i=1:length(BlinkSet)

      xAnchorID=30;
      xSourceID=50;
      a=BlinkSet{i}.AnchorID;
      b=BlinkSet{i}.SourceID;
      if xAnchorID==a && xSourceID==b
      xagc=BlinkSet{i}.agc;
      rssi_dB(i)=-(33+xagc*(89-33)/(29-1));
      end
  end
     rssi_dB(rssi_dB==0)=[];
     rssi_dBm=sum(rssi_dB(:))/length(rssi_dB);
     disp([sprintf('The rssi value is %0.0f',rssi_dBm)]);

When I call the function in Matlab command window I get the rssi value of the above function.

Also my task is when I reciprocate the Anchor ID and source ID say Anchor as 50 and source as 30 like the function I have mentioned below I get an error which is mentioned after the function below.

function A50(BlinkSet)
for i=1:length(BlinkSet)

   xAnchorID=50;
   xSourceID=30;
   a=BlinkSet{i}.AnchorID;
   b=BlinkSet{i}.SourceID;
   if xAnchorID==a && xSourceID==b
   xagc=BlinkSet{i}.agc;
   rssi_dB(i)=-(33+xagc*(89-33)/(29-1));
   end

end
rssi_dB(rssi_dB==0)=[];
   rssi_dBm=sum(rssi_dB(:))/length(rssi_dB);
disp([sprintf('The rssi value is %0.0f',rssi_dBm)]);

When I call this function I get an error as

??? Undefined function or variable "rssi_dB".

 Error in ==> A50 at 14
 rssi_dB(rssi_dB==0)=[];

 Error in ==> main_reduced at 26
 A50(BlinkSet);

In main function I have coded like this,

 %A30(BlinkSet);
 A50(BlinkSet);

Any help is highly appreciated.

Was it helpful?

Solution

In both of these functions, you only create the variable rssi_dB if execution enters the if statement within the loop (i.e., if xAnchorID==a && xSourceID==b is at some point true). Clearly, this code is never executed in your A50 function. Without knowing what is in BlinkSet it's a bit difficult to diagnose the exact problem, but this is the cause at least.

As a side note: it's not a good idea to create two separate functions to do this job when their code is almost identical. You should add an input argument to your function that allows it to do the job of both. In this particular case, all that changes is the value of xAnchorID and xSourceID, so you could just pass these in:

function srcToAnchorRssi(BlinkSet, xSourceID, xAnchorID)
    % The rest of the function stays the same!

If you want to provide some defaults for these arguments, you can do, e.g.:

if nargin < 3 || isempty(xAnchorID), xAnchorID = 50; end
if nargin < 2 || isempty(xSourceID), xSourceID = 30; end

It's always a good idea to include an isempty in statements of this sort, so that your function supports syntax like myFunction(myArg1, [], myArg3). Also note that the order of the operands to || is crucial; if you did if isempty(theArgument) || nargin < theArgumentNumber and the user did not pass theArgument, then it would error in the isempty because theArgument would not exist as a local variable. We can get around this by swapping the operands' order because MATLAB is smart enough to know it doesn't have to evaluate the right operand if the left operand is true (note that this is also the case in many other programming languages).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top