Output argument is not assigned on some execution paths for Embedded matlab function

For a single input u to the embedded matlab function using the following code:
if u>0
y=1;
end
if u<0
y=-1;
end
if u==0
y=0;
end
gives error: "output argument 'y' is not assigned on some execution paths."
I don't know why the codes above are not working. Instead, I have to use if...elseif...else structure.

Respuestas (3)

Cedric
Cedric el 11 de En. de 2013
Editada: Cedric el 11 de En. de 2013
Look at the following:
>> u = [1 2 3] ;
>> if u < 2, fprintf( 'OK\n' ) ; end
>> if u > 2, fprintf( 'OK\n' ) ; end
>> if u == 2, fprintf( 'OK\n' ) ; end
>> if u == [1 2 3], fprintf( 'OK\n' ) ; end
OK
>>
EDIT: Also, and the error might come from that..
>> [NaN < 0, NaN > 0, NaN == 0]
ans =
0 0 0
Testing equalty of arrays, or arrays/scalar, requires a little more work usually. Look up:
>> doc any
>> doc all
>> doc isnan
>> doc isequalwithequalnans
EDIT: in your case with the error, you should probably manage NaN cases. If M-Lint were indicating (as a tip) that the output arg. might not be assigned, I would guess that the array/scalar tests could be a cause.
EDIT: if you know that u is a scalar (this should be tested if you wanted to create a robust function) and not NaN, just build an if u>0, .. elseif u<0, .. else.. end conditional statement. It is better than your series of statements in the sense that it doesn't test conditions from statements below one whose condition was true.
Cheers,
Cedric

5 comentarios

Qingbin
Qingbin el 11 de En. de 2013
Editada: Qingbin el 11 de En. de 2013
The code that I wrote works well for .m file. But it doesn't work for embedded matlab function.
You are right in that sense! But I think the series of statements should also be working while they turned out to be invalid for some reason.
Define an output value for NaN and/or non-scalar cases, or an error() that will tell you in which case you are falling. If I was developing a function (I don't know how that translates to embedded functions), I would do something like:
if isnan(u), error('NaN input not allowed.') ; end
if ~isscalar(u), error('Non-scalar inputs not allowed.') ; end
or
if isnan(u)
y = 0 ; % Set y=0 for NaN inputs (..).
end
Qingbin
Qingbin el 11 de En. de 2013
Editada: Qingbin el 11 de En. de 2013
I have added the code to test if the input is NAN or not, the error is still there. Actually for my case, the input u is just a scalar (a positive integer). But thanks for the consideration for multiple cases.
I have the same problem. Please share how you solved it finally.
FYI, I used if...elseif...end structure instead of if...end...if end structure.

Iniciar sesión para comentar.

How about:
y = sign(u)
?

1 comentario

I am trying to test the assignment structure for the embedded matlab function. So it doesn't necessarily be sign (u).

Iniciar sesión para comentar.

Have you fixed this? I have similar problem

4 comentarios

You need to cover all of the cases, including taking into account that the input might be a vector or that the matrix might contain nan or inf.
Or, you need to initialize first to a value that should be used if none of the conditions hold.
Or you need to use an if/elseif/else structure ending with else to catch the final possibility that nothing else matched
Or you need to use switch with an Otherwise case.
Hi
I have the same problem in Simulink, according to the following message:
Output argument 'segSNR' is not assigned on some execution paths.
Function 'Signal to Noise Ratio/Signal to Noise Ratio' (#114.23.29), line 1, column 24:
"segSNR"
the MATLAB code is:
function [overall_snr, segSNR] = Signal_to_Noise_Ratio(Speech, Enhanced_speech,sample_rate)
% ----------------------------------------------------------------------
% Check the length of the clean and processed speech. Must be the same.
% ----------------------------------------------------------------------
clean_length = length(Speech);
Enhanced_length = length(Enhanced_speech);
if (clean_length ~= Enhanced_length)
disp('Error: Both Speech Files must be same length.');
return
end
% ----------------------------------------------------------------------
% Scale both clean speech and processed speech to have same dynamic
% range. Also remove DC component from each signal
% ----------------------------------------------------------------------
%clean_speech = clean_speech - mean(clean_speech);
%processed_speech = processed_speech - mean(processed_speech);
%processed_speech = processed_speech.*(max(abs(clean_speech))/ max(abs(processed_speech)));
overall_snr = 10* log10( sum(Speech.^2)/sum((Speech-Enhanced_speech).^2));
% ----------------------------------------------------------------------
% Global Variables
% ----------------------------------------------------------------------
% sample_rate = 8000; % default sample rate
% winlength = 240; % window length in samples
% skiprate = 60; % window skip in samples
winlength = fix(30*sample_rate/1000); %240; % window length in samples
skiprate = floor(winlength/4); % window skip in samples
MIN_SNR = -10; % minimum SNR in dB
MAX_SNR = 35; % maximum SNR in dB
% ----------------------------------------------------------------------
% For each frame of input speech, calculate the Segmental SNR
% ----------------------------------------------------------------------
num_frames = clean_length/skiprate-(winlength/skiprate); % number of frames
start = 1; % starting sample
window = 0.5*(1 - cos(2*pi*(1:winlength)'/(winlength+1)));
for frame_count = 1: num_frames
segmental_snr = zeros(1,num_frames);
% ----------------------------------------------------------
% (1) Get the Frames for the test and reference speech.
% Multiply by Hanning Window.
% ----------------------------------------------------------
clean_frame = Speech(start:start+winlength-1);
processed_frame = Enhanced_speech(start:start+winlength-1);
clean_frame = clean_frame.*window;
processed_frame = processed_frame.*window;
% ----------------------------------------------------------
% (2) Compute the Segmental SNR
% ----------------------------------------------------------
signal_energy = sum(clean_frame.^2);
noise_energy = sum((clean_frame-processed_frame).^2);
segmental_snr(frame_count) = 10*log10(signal_energy/(noise_energy+eps)+eps);
segmental_snr(frame_count) = max(segmental_snr(frame_count),MIN_SNR);
segmental_snr(frame_count) = min(segmental_snr(frame_count),MAX_SNR);
start = start + skiprate;
segSNR= mean( segmental_snr);
end
Could you please tell me how I can solve this?
Thanks in advance
Cause: if num_frames is less than one then segSNR is never defined.
Solution: put a dummy value at the start of the function, e.g. segSNR = [];
Tip: you should avoid using global variables.
Your section
if (clean_length ~= Enhanced_length)
disp('Error: Both Speech Files must be same length.');
return
end
does not assign to some output variables. You can switch to error() instead of disp/return, or you can switch to assert() instead of disp/return, or you can assign outputs to all of the variables for that case.
Also, your segSNR is not assigned to unless at least one iteration of the for loop is done; the way you calculate num_frames it can come out less than 1.
Question: why are you overwriting all of segSNR on each iteration of the loop?

Iniciar sesión para comentar.

Categorías

Más información sobre Simulation, Tuning, and Visualization en Centro de ayuda y File Exchange.

Preguntada:

el 11 de En. de 2013

Editada:

el 24 de Oct. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by