Strange case of Code analyzer and if-else block
    7 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
    AlireaA Mirrabie
 el 20 de Sept. de 2024
  
    
    
    
    
    Comentada: AlireaA Mirrabie
 el 21 de Sept. de 2024
            While running a simple if-else statement, I encountered a rather strange situation. In one part of my code, I have a section that generates a signal based on user choice via a dropdown menu. The code is referenced from the @CES_matlabinthemiddleeast YouTube channel, but I have made some changes. Here’s the relevant part of the code:
Fs = 1000;      % Sampling rate
T = 1/Fs;       % Sampling time
t = 0:T:1-T;    % Time vector
mySig = zeros(1, length(t));    % Preallocation for generated signal
frq = [20, 40, 80, 160];        % Frequency vector
lenFrq = numel(frq);            % Length of frequency vector
sigType = 'stationary';
% sigType = 'non-stationary'; % just imagin it's a drop down menu.
if strcmp(sigType, 'stationary')
    mySig = 0.25 * sum(cos(2 * pi * frq.' * t), 1);  
else
    for idx = 1:lenFrq
        start = 250 * (idx - 1) + 1;  % Start index
        stop = min(250 * idx, length(t));  % Stop index; ensure it does not exceed length(t)
        if start <= length(t) && stop <= length(t)
            mySig(start:stop) = cos(2 * pi * frq(idx) * t(start:stop));
        end
    end
end
The minor issue I’m experiencing is that, based on the selected sigType, the MATLAB code analyzer flags either the if block or the else block with a warning stating, "This statement (and possibly following ones) cannot be reached." It’s rather obvious that only one of the blocks will execute, and it seems redundant for the analyzer to flag this. I'm only asking this question because my OCD doesn't allow me to ignore that annoying little yellow warning ribbon.
Here's the photo to further clarify the issue:

0 comentarios
Respuesta aceptada
  Steven Lord
    
      
 el 20 de Sept. de 2024
        It’s rather obvious that only one of the blocks will execute, and it seems redundant for the analyzer to flag this.
In this circumstance, yes it is pretty clear to a human reading the code that only one of the blocks will execute. The line that defines which block will be executed occurs immediately before the if statement.
But in general it may not always be so obvious why MATLAB can prove to itself through static analysis that a line will not be executed, that it is dead code. So Code Analyzer will flag that scenario whenever it sees it.
If you know that the Code Analyzer message can safely be ignored, such as when you understand the scenario it's warning about and intended to use that behavior (like intentionally leaving off a semicolon to display a variable's contents), you can suppress it. See the "Adjust Code Analyzer Message Indicators and Messages" section on this documentation page.
Más respuestas (0)
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

