MATLAB Answers

UI function error because of version difference

3 views (last 30 days)
Paul Mitchell
Paul Mitchell on 23 Jul 2020
I was given a block of code just to mess with for fun that is a live audio guitar tuner, and it is throwing a UI error for version differences in which this syntax no longer works. The version of MATLAB this was written on is so old (I'm not sure which version, but it works on 2011) that I am having a hard time really pinning down the change that needs to happen in order to make it work. Upon running, the UI pops up like it should, but when I try to use the record function to start the process, there is a logic error being caused by the difference in version (I have both 2019 and 2020 and it gives the same error). I assume the change needed isn't a very complicated one because it usually isn't, but I am having a hard time with it. Could you guys give some input?
function varargout = GuitarTuner(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @GuitarTuner_OpeningFcn, ...
'gui_OutputFcn', @GuitarTuner_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% Opening segment just before GUI is visible
function GuitarTuner_OpeningFcn(hObject, ~, handles, varargin)
handles.output = hObject;
guidata(hObject, handles);
load ButtonIcons;
set(handles.playbutton1,'CData',PlayDisabled);
set(handles.recbutton1,'CData',RecDisabled);
disp('GuitarTuner');
function varargout = GuitarTuner_OutputFcn(~, ~, handles)
varargout{1} = handles.output;
function freq1_Callback(~, ~, ~)
% Object creation after setting properties
function freq1_CreateFcn(hObject, ~, ~)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% End GUI code
% Begin tuning functions
function outstr = getNotes(tone)
notes = {'C','C#','D','D#','E','F','F#','G','G#','A','A#','B'};
for n = 1:length(tone)
octave = floor((tone(n)-1)/12);
note = mod(tone(n)-1,12)+1;
outstr{n} = [notes{note},num2str(octave)];
end
function tune(~, handles)
freq = str2double(get(handles.freq1,'String'));
tone = round(log(freq/440)/0.0578+58);
tones = (tone-2:tone+2);
freqs = 15.43379*exp(0.05776234*tones);
notes = getNotes(tones);
axes(handles.calaxes1);
semilogx(freq,1,'*');
xlim([freqs(1) freqs(end)]);
set(handles.calaxes1,'YTickLabel',[],'XTick',freqs,'XTickLabel',notes,'XGrid','on');
Fs = 96000*2;
twindow = 0.5;
r = audiorecorder(Fs,16,1);
data = linspace(0,1,twindow*Fs)';
latestfit = 0;
while gcbo % Handle of object whose callback is executing
if get(handles.recbutton1,'UserData')==0
break;
end
freq = str2double(get(handles.freq1,'String'));
tone = round(log(freq/440)/0.0578+58);
tones = (tone-2:tone+2);
freqs = 15.43379*exp(0.05776234*tones);
notes = getNotes(tones);
% [b,a] = butter(2,[freqs(1) freqs(end)]/(Fs/2));
record(r);
% Process previous data while recording new data
L = length(data);
NFFT = 2^nextpow2(L);
fftdata = abs(fft(data,NFFT)/L);
fftdata = fftdata(1:floor(length(fftdata)/2));
x = linspace(0,Fs/2,length(fftdata))';
indx = and(x>freqs(1),x<freqs(end));
npts = sum(indx);
xclip = x(indx);
% disp(['Freq res: ',num2str(xclip(2)-xclip(1)),' Hz']);
fftclip = fftdata(indx);
[mag,i] = max(fftclip);
nbrs = 4; % neighboring points for curve fit
nbrs2 = 2; % points on either side
% disp([i,npts,nbrs,mag/mean(fftclip)]);
if i>(nbrs+1) && i<npts-(nbrs+1) && (mag/mean(fftclip))>1.5
% fit exponential normal distribution near peak
P = polyfit(xclip(i-nbrs2:i+nbrs2)-xclip(i),log(fftclip(i-nbrs2:i+nbrs2)),2);
if P(1)<0 % check for inverted fit curve
x2 = linspace(xclip(i-nbrs),xclip(i+nbrs),20)-xclip(i);
y2 = exp(polyval(P,x2));
fmax = -P(2)/2/P(1)+xclip(i);
semilogx(xclip,fftclip,'b',x2+xclip(i),y2,'c',[1 1]*fmax,[0 20],'r','LineWidth',2);
if fmax > freqs(1) && fmax < freqs(end)
latestfit = fmax;
% check tune
flat = 15.43379*exp(0.05776234*(tone - 0.1));
sharp = 15.43379*exp(0.05776234*(tone + 0.1));
if fmax > flat && fmax < sharp
hold on;
semilogx([1 1]*latestfit,[0 20],'g','LineWidth',3);
hold off;
end
end
else % trash it if inverted curve
fpk = xclip(i);
semilogx(xclip,fftclip,'b');
end
else
end
axis([freqs(1) freqs(end) 0 1.2*max(mag,0.005)]);
set(handles.calaxes1,'YTickLabel',[],'XTick',freqs,'XTickLabel',notes,'XGrid','on');
pause(twindow);
stop(r);
data = getaudiodata(r,'double');
% data = filtfilt(b,a,data);
end
function figure1_ButtonDownFcn(~, ~, ~)
function playbutton1_Callback(hObject, ~, handles)
load ButtonIcons;
Fs = 96000;
duration = 1;
set(hObject,'CData',PlayEnabled);
freq = str2double(get(handles.freq1,'String'));
t = (0:1/Fs:duration);
sig = sin(2*pi*freq*t);
soundsc(sig,Fs);
pause(duration);
set(hObject,'CData',PlayDisabled);
function recbutton1_Callback(hObject, eventdata, handles)
load ButtonIcons;
if get(hObject,'UserData')
% if on, turn off
set(hObject,'UserData',0);
set(hObject,'CData',RecDisabled)
else
% if off, turn on
set(hObject,'UserData',1);
set(hObject,'CData',RecEnabled)
tune(eventdata, handles);
end
function popupmenu1_Callback(hObject, ~, handles)
tones = [41 46 51 56 60 65 ];
selection = get(hObject,'Value');
if selection < 7,
set(handles.freq1,'String',num2str(15.43379*exp(0.05776234*tones(selection)),'%3.2f'));
set(handles.freq1,'Enable','off');
else
set(handles.freq1,'Enable','on');
end;
function popupmenu1_CreateFcn(hObject, ~, ~)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
Command Window
Conversion to logical from matlab.ui.control.UIControl is not possible.
Error in GuitarTuner>tune (line 87)
while gcbo % Handle of object whose callback is executing
Error in GuitarTuner>recbutton1_Callback (line 184)
tune(eventdata, handles);
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in GuitarTuner (line 20)
gui_mainfcn(gui_State, varargin{:});
Error while evaluating UIControl Callback.

  0 Comments

Sign in to comment.

Answers (1)

Harsha Priya Daggubati
Harsha Priya Daggubati on 27 Jul 2020
Hi,
It is very difficult to track down the cause of the error just using the code you pasted. It would help if you can provide me with the proper error message you are getting.
You can als try using the following link that helps your app code compatible with newer versions:

  0 Comments

Sign in to comment.

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by