How to pass string value from one callback function to another?
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
mohanish
am 22 Apr. 2018
Kommentiert: mohanish
am 2 Mai 2018
I am typing a code where in one callback function I am browsing for a audio file (.wav) by hitting one pushbutton and setting the filename in one of the edit box (edit6). Now, I want this audio file to be read when I hit the second pushbutton. What command should I use? so that I can read the audio file which i selected and plot its fft and time domain? Here's the code..
% --- Executes on button press in pushbutton7.
function pushbutton7_Callback(hObject, eventdata, handles)
[filename,filepath]= uigetfile({'*.*';'*.wav';'*.m4a'});
fullname = [filepath filename];
set(handles.edit6,'string', filename) ;
% --- Executes on button press in button.
function button_Callback(hObject, eventdata, handles)
% hObject handle to button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
data= audioread('C:\Users\Mohanish\Desktop\GA\Project\mohanish.wav');
L= length (data);
Y= fft(data)
P2= abs (Y/L)
P1= P2(1:L/23+1);
P1(2:end-1)= 2*P1(2:end-1)
figure= subplot('position', [0.56, 0.15, 0.4, 0.3]);
K= plot (P1);
title ('Frequency Domain')
xlabel('f(Hz)')
ylabel('|P1(f)|')
hold on
[y,Fs]= audioread('C:\Users\Mohanish\Desktop\GA\Project\mohanish.wav');
t= linspace(0,length(y)/Fs, length(y));
figure1= subplot('position', [0.10, 0.15, 0.4, 0.3]);
plot(t,y);
title ('Time Domain')
xlabel('Time (sec)')
ylabel('Amplitude')
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 22 Apr. 2018
Change
fullname = [filepath filename];
to
fullname = fullfile(filepath, filename);
Change
data= audioread('C:\Users\Mohanish\Desktop\GA\Project\mohanish.wav');
to
fullname = get(handles.edit6, 'string');
if isempty(fullname)
warndlg('You need to select a file name');
return;
end
if ~exist(fullname, 'file')
warndlg(sprintf('Odd, file does not exist, "%s", fullname));
return;
end
try
data = audioread(fullname);
catch ME
warndlg(sprintf('File is not a valid audio file: "%s"', fullname);
return
end
20 Kommentare
Walter Roberson
am 1 Mai 2018
No, it does plot. But you should take out the "hold on" at line 305.
Just above that you have
P1= P2(1:L/23+1);
Why are you assuming that the length is exactly divisible by 23? Why are you plotting only the first 1/23 of the frequencies? Did you mean /2 instead of /23 ? (Even then you should not assume that L is even.)
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Whos finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!