MATLAB Coder: Runtime Error due to 'Undefined Variable'
2 views (last 30 days)
Show older comments
Vasileios Leivadas
on 9 Nov 2019
Commented: Kavya Vuriti
on 15 Nov 2019
I am using MATLAB coder in order to generate a .dll C file from my MATLAB code. Upon the runtime error check I am getting a number of errors for different variables which are all ultimately based on this one:
% Undefined function or variable 'b'. The first assignment to a local variable determines its class.
For this particular error my respective code segment is the following:
% Butterworth
fc = [0.5,1,2,3];
fs = 10;
order = 8;
postbutt = {}; % 1st channel butterworth filtered
filtsigs = zeros(chansize,numchannels); % Butt @ fc = 1Hz
b = zeros(1,order+1); a = zeros(1,order+1);
for f = 1:length(fc)
[b,a] = butter(order, fc(f)/(fs/2));
postbutt{f} = filtfilt(b, a, freq_ch1);
if fc(f) == 1
for chan = 1:numchannels
filtsigs(:,chan) = filtfilt(b, a, freq_ch1);
end
end
end
As it's apparent from the code, I've tried to define and initialize my variables a and b before their actual value assignment in the for loop, in order to avoid the error but that didn't seem to resolve the issue. I've looked around for similar questions in the central but couldn't find an appropriate solution. If anyone has any thoughts on this one it'd be much appreciated!
2 Comments
Accepted Answer
Kavya Vuriti
on 13 Nov 2019
Hi,
From the code provided, I noticed that the second argument to ‘butter’ function keeps changing for every iteration. For code generation support, filter coefficients must be constant, and values of expressions or variables should not change. You can refer this for limitations of code generation for butter function. If the number of values variable fc takes is small, a possible workaround could be:
% Butterworth
fc = [0.5,1,2,3];
fs = 10;
order = 8;
% An empty cell array of size 1x4
postbutt = cell(1, 4); % 1st channel butterworth filtered
filtsigs = zeros(chansize,numchannels); % Butt @ fc = 1Hz
b = zeros(1,order+1);
a = zeros(1,order+1);
[b,a] = butter(order, fc(1)/(fs/2));
postbutt{1} = filtfilt(b, a, freq_ch1);
[b,a] = butter(order, fc(2)/(fs/2));
postbutt{2} = filtfilt(b, a, freq_ch1);
for chan = 1:numchannels
filtsigs(:,chan) = filtfilt(b, a, freq_ch1);
end
[b,a] = butter(order, fc(3)/(fs/2));
postbutt{3} = filtfilt(b, a, freq_ch1);
[b,a] = butter(order, fc(4)/(fs/2));
postbutt{4} = filtfilt(b, a, freq_ch1);
Hope this helps.
4 Comments
Kavya Vuriti
on 15 Nov 2019
Hi,
Variable inputs to butter function is currently not supported by MATLAB Coder. As of now, if you want to allow multiple cut-off frequencies, you must have function call for each cut-off frequency in your code.
Also, I have heard that this issue is known and concerned parties may be investigating further.
More Answers (0)
See Also
Categories
Find more on Code Generation and Deployment in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!