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

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 日

0 投票

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 件のコメント

mohanish
mohanish 2018 年 4 月 22 日
編集済み: Walter Roberson 2018 年 4 月 22 日
% --- Executes on button press in pushbutton7.
function pushbutton7_Callback(hObject, eventdata, handles)
[filename,filepath]= uigetfile({'*.*';'*.wav';'*.m4a'});
fullname = fullfile(filepath, filename);
set(handles.edit6,'string', fullname);
% --- 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)
fullname = get(handles.edit6, 'string');
data = audioread(fullname);
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(fullname);
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')

I am having a problem. when i get the back button in this .fig and come back and try to browse the .wav file, it does not set the fullname in the edit text box.

Walter Roberson
Walter Roberson 2018 年 4 月 22 日

Put in a breakpoint at the uigetfile and step through to see what happens. For example is handles.edit6 still a valid handle at the time of the set() ? If the set() works without error then what do you get if you then (in the debugger)

get(handles.edit6, 'string')
mohanish
mohanish 2018 年 4 月 22 日
Hi Walter, It runs properly for the first time. It browses the file and plots the graphs. But, after I hit the back button and come back and try to the same thing again it does not set the file fullname in edit box and eventually does not plot the graph.
mohanish
mohanish 2018 年 4 月 23 日
編集済み: Walter Roberson 2018 年 4 月 23 日

this is error the message that I am getting..

Undefined function or variable 'button_DeleteFcn'.
Error in gui_mainfcn (line 95)
        feval(varargin{:});
Error in multiply (line 43)
    gui_mainfcn(gui_State, varargin{:});
Error in matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)multiply('button_DeleteFcn',hObject,eventdata,guidata(hObject))
Error using multiply>figure1_CloseRequestFcn (line 140)
Error while evaluating UIControl DeleteFcn.
Dot indexing is not supported for variables of this type.
Error in multiply>pushbutton7_Callback (line 248)
set(handles.edit6,'string', fullname);
mohanish
mohanish 2018 年 4 月 23 日
Even after putting the breakpoint at the uigetfile line, i still cannot set the fullname in the edit box
Walter Roberson
Walter Roberson 2018 年 4 月 23 日
Attach your code and fig file
mohanish
mohanish 2018 年 4 月 23 日
here are the files..
Walter Roberson
Walter Roberson 2018 年 4 月 23 日

Your uicontrol named button with string Train! has been configured to expect to be able to call button_DeleteFcn and button_KeyPressFcn but those do not exist.

mohanish
mohanish 2018 年 4 月 23 日
I deleted the button_DeleteFcn of the train button.. still it is not working.. Can you correct it and attach the file here?
Walter Roberson
Walter Roberson 2018 年 4 月 23 日
I have a couple of other things to do first; I will work on it again later.
mohanish
mohanish 2018 年 4 月 23 日
Yes. No problem.. Take your time. Even I will work on it! Thank You for the Help anyways!
mohanish
mohanish 2018 年 4 月 24 日
How to remove the error saying that dot indexing is not supported for this type of variable?
Dot indexing is not supported for variables of this type.
Error in multiply>pushbutton7_Callback (line 239)
set(handles.edit6,'string', fullname);
Walter Roberson
Walter Roberson 2018 年 4 月 24 日
Revisions enclosed.
mohanish
mohanish 2018 年 4 月 24 日
Hey Walter, It is still having the same problem.. it is showing the same error which I have mentioned above..
Walter Roberson
Walter Roberson 2018 年 4 月 24 日

Please make sure you are using my versions of the .fig and the .m . Close any existing figures and "clearvars"

The fix for the back button (which has already been written into that multipy.m) was

function pushbutton2_Callback(hObject, eventdata, handles)
back_to = 'SRS01_GUI_TrainPage.fig';
if exist(back_to, 'file')
    open(back_to);
    close('multiply');
else
    waitfor(warndlg( sprintf('Cannot go back, did not find find "%s"', back_to)));
end

That is, the reason you were failing was that the "back" button was trying to invoke SRS01_GUI_TrainPage and that did not exist. The replacement code makes sure it exists before trying to invoke it.

mohanish
mohanish 2018 年 4 月 25 日
Hey Walter, I have a fig file named SRS01_GUI_TrainPage.. I have attached it here.. Hit the back button and it opens that page.. coming back on the multiply fig after hitting train new user.. it does not set the fullname of the browsed file..
Walter Roberson
Walter Roberson 2018 年 4 月 25 日
In your code, you attempt to switch control to multiply by using
open('multiply.fig');
and you attempt to switch control back from multiply by using
open('SRS01_GUI_TrainPage.fig')
or
open('Untitled.fig')
What you need to know about this is that opening a .fig file does not run the associated .m file, and it is the .m file that creates the appropriate handles structures. If you want to switch back to SRS01_GUI_TrainPage then instead of
open('SRS01_GUI_TrainPage.fig')
you need to have
SRS01_GUI_TrainPage;
mohanish
mohanish 2018 年 5 月 1 日
Hey Walter, I checked all the lines and changed the new muiltply file to multiply1 in code. still i cannot get the plots of the .wav file after i get from train page to multiply. I have attached the m and fig files.
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 件)

カテゴリ

ヘルプ センター および File ExchangeCode Execution についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by