MATLAB Coder: Runtime Error due to 'Undefined Variable'

9 Ansichten (letzte 30 Tage)
Vasileios Leivadas
Vasileios Leivadas am 9 Nov. 2019
Kommentiert: Kavya Vuriti am 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 Kommentare
KALYAN ACHARJYA
KALYAN ACHARJYA am 9 Nov. 2019
chansize?
numchannels?
Vasileios Leivadas
Vasileios Leivadas am 9 Nov. 2019
They are all defined previously in the code. These ones are doubles. I can't post the whole program because most of it is out of context. Assume all of them are defined.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Kavya Vuriti
Kavya Vuriti am 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 Kommentare
Vasileios Leivadas
Vasileios Leivadas am 14 Nov. 2019
To be more specific this is the code snippet I was referring to:
NumVars = width(tablein);
varlen = height(tablein);
varType = repmat({class(tablein{1,1}(1))},NumVars,1);
varNames = tablein.Properties.VariableNames;
tablesize = [varlen NumVars];
tableout = table('Size',tablesize,'VariableTypes',varType,'VariableNames',varNames);
for i = 1:NumVars
len = length(tablein{1,i});
transval = tablein{1,i}.';
if len < varlen
extraval = mean(tablein{1,i}(end-1:end));
transval(varlen) = extraval;
end
tableout.(i) = transval;
end
'tablein' is just a table with variable size cells as its elements. I am getting this Runtime Error:
% Variable indices into a table must be constant.
I guess this is the same error as above but on this occasion due to the variable's size it wouldn't be ideal to instantiate every element in the table explicitly. Any ideas?
Kavya Vuriti
Kavya Vuriti am 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu MATLAB Coder finden Sie in Help Center und File Exchange

Produkte


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by