How to pass string value from one callback function to another?

조회 수: 4 (최근 30일)
mohanish
mohanish 2018년 4월 22일
댓글: mohanish 2018년 5월 2일
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')

채택된 답변

Walter Roberson
Walter Roberson 2018년 4월 22일
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
Walter Roberson
Walter Roberson 2018년 5월 1일
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.)
mohanish
mohanish 2018년 5월 2일
Hey Walter, It is still showing me this problem..
Dot indexing is not supported for variables of this type.
Error in multiply1>pushbutton7_Callback (line 268)
set(handles.edit6,'string', fullname);

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by