About Matlab Gui (push button)

1 view (last 30 days)
Huseyin
Huseyin on 26 Feb 2014
Commented: Huseyin on 26 Feb 2014
Hello, I have a problem with matlab gui. I use a push button to get a file and I want to open it using another push button but I couldn't do that. The code is;
if true
% code
function open_Callback(hObject, eventdata, handles)
% hObject handle to open (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[dosyaadi,dosyayolu]=uigetfile('name.mid','Select a file');
if dosyaadi==0
return
end
nmat=readmidi_java(fullfile(dosyayolu,dosyaadi));
handles.nmat=nmat;
guidata(hObject,handles);
dosyaadi
end
Until that, everything is ok. I just want to use "dosyaadi" ,which is the original name of nmat, in another push button with the function 'system' but it didn't work. This is very normal because I didn't define what 'dosyaadi' is in that push button. How can I do that? I am new at matlab GUI, so I need a help!
if true
% code
function listen_Callback(hObject, eventdata, handles)
% hObject handle to listen (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
nmat=handles.nmat;
system(dosyaadi);
end

Answers (1)

Image Analyst
Image Analyst on 26 Feb 2014
You get this from the first function: [dosyaadi,dosyayolu], which means dosyaadi is the base filename that the user chose, and dosyayolu is the folder that the user chose to store the file in. Ok, fine. Then you pass that full filename into readmidi_java() and accept the result into a variable called nmat. Then you say ""dosyaadi" ,which is the original name of nmat" - which I don't understand. Why does it have an original name, and a new name called nmat? Is nmat the base filename that has somehow been altered, based on the original value of dosyaadi? OK, whatever. Then you attach it to handles as a new field. That's fine. Then you call guidata so that handles will have that value inside any other function that access the handles structure. OK good. A bit strange, but okay.
Then you have a second function listen_Callback(). First it gets nmat. This will work because nmat is properly attached to handles. Then it calls system:
system(dosyaadi);
Well dosyaadi has not been defined inside listen_Callback(). It was only a local variable inside open_Callback() and vanished when you exited that function. So listen_Callback() will say that there is no dosyaadi variable when you call system(). To get it, you must have this line in both functions
global dosyaadi;
or else use setappdata and setappdata() like in the FAQ: http://matlab.wikia.com/wiki/FAQ#How_can_I_share_data_between_callback_functions_in_my_GUI.28s.29.3F There are some other methods there in the FAQ. If you want, you can just attach dosyaadi to handles like you did for nmat - you already know how to do that so that may be simplest for you.
  2 Comments
Huseyin
Huseyin on 26 Feb 2014
Thank you so much that I figured out how to do that but there is a problem in "global dosyaadi". It works but when I call another new .mid file (with uigetfile), "system(dosyaadi)" plays the old one. I mean the data in "dosyaadi" does not change because of the line "global dosyaadi". How can I fix that?
And I want to ask one more question. I tried to solve this problem with another way you said. When I try to attach dosyaadi to handles like I did for nmat, matlab says "undefined function or variable dosyaadi". As a result, "handles.nmat" works but "handles.dosyaadi" doesn't work. Did I do something wrong?
Thanks
Huseyin
Huseyin on 26 Feb 2014
I fixed the problem in my first question. The mistake was I put "global dosyaadi" line to the wrong line. Now it works. But I didn't find the solution of the second question. If help about it, I will be very greatful

Sign in to comment.

Categories

Find more on MATLAB Compiler in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!