Output argument is not assigned on some execution paths for Embedded matlab function
Mostrar comentarios más antiguos
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)
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
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
maryam
el 28 de En. de 2014
I have the same problem. Please share how you solved it finally.
Qingbin
el 31 de En. de 2014
Sean de Wolski
el 11 de En. de 2013
How about:
y = sign(u)
?
1 comentario
Qingbin
el 11 de En. de 2013
Dr Yu Lu
el 21 de Oct. de 2016
0 votos
Have you fixed this? I have similar problem
4 comentarios
Walter Roberson
el 22 de Oct. de 2016
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.
Heawa Mehmandoust
el 24 de Oct. de 2017
Editada: Walter Roberson
el 24 de Oct. de 2017
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
Stephen23
el 24 de Oct. de 2017
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.
Walter Roberson
el 24 de Oct. de 2017
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?
Categorías
Más información sobre Simulation, Tuning, and Visualization en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!