make a GUI for an existing programme

23 visualizaciones (últimos 30 días)
Rebecca
Rebecca el 16 de Jul. de 2013
So I've written a reasonably elegant piece of code where you input three to five formant frequencies (speech signal processing) and it generates an area function, and outputs a figure of the spectrum and the area function.
I've watched the tutorial and I have no idea how to make a GUI past making some sliders.
I want the sliders to be used to input the frequencies, then the figures generated to be displayed.
How do I do this? - do I have to re-write my program to fit into the GUI callback thing or can I somehow link it?
also the existing program already calls a few functions and opens a matlab data file (in case that makes a difference)
Thanks!

Respuestas (3)

Evan
Evan el 16 de Jul. de 2013
Editada: Evan el 16 de Jul. de 2013
Whether you copy and paste your code into a callback of your GUI or call it from within the gui, it's going to require some modifications. Instead of inputs to your function from the command window, you're going to have to obtain the "Value" properties of your sliders (and then you'll have to scale them to meet the data ranges you're wanting).
So, while previously your function might have been:
function out = my_function(a,b,c)
You'll have the mfile created by GUIDE, and in the callback where you want the magic to happen you'll have to have code resembling the snippet below. This example assumes that the user hits a button to execute your code. At that point, the callback of the pushbutton obtains the current values of all your sliders.
function my_buttonCallback(hObject,eventdata,handles)
a = get(handles.slider1,'Value');
b = get(handles.slider2,'Value');
c = get(handles.slider3,'Value');
It works this way for many other uicontrols, though for editboxes you'll be using the "String" property instead of "Value," and for other, more complex controls like listboxes (if you end up using them), you'll have to use a combination of both to get in your user-specified parameters.
Beyond that, it's hard to offer much advice unless you are experiencing any specific difficulties. I will suggest, however, getting to understand the handles structure and guidata. These two concepts are very important for making an efficient, easy to read/modify GUI. So, if you haven't read the below link yet, you'll probably find it helpful:
  1 comentario
Evan
Evan el 16 de Jul. de 2013
To add to that, "slider1," "slider2," and "slider3" are all the "Tags" for each of your sliders. If you open up the property inspector while in guide, you can set this "Tag" property to whatever name you like. Upon saving, your mfile will automatically be updated so that the callback names match this tag.
If you want to access any uicontrol, you have to find it in the handles structure by its tag name. That is what the above code is doing.

Iniciar sesión para comentar.


Iain
Iain el 16 de Jul. de 2013
You can make a gui do almost anything, if you're willing to put in the effort to make it work the way you want it to. "guide" is useful (type guide at command line).
A commandbutton can be used to run a bit of code. - such as getting parameters from the controls on the GUI, and sending them to a program as parameters, eg.
function My_specialcallback(handles,eventdata .... )
the_input = get(handles.An_Edit_box_with_a_filename_in_it,'string');
[the_output1, the_output2 ... ] = my_function(the_input);
set(handles.An_Edit_box_for_output,'string',the_output1)
set(handles.A_check_box,'value',the_output2)
end

Andrew Reibold
Andrew Reibold el 16 de Jul. de 2013
You can use your GUI to call existing functions, but I believe they need to be saved in the same folder as where you have your GUI saved.
I strongly recommend using 'guide' for a simple task like what you have. Its really hard to explain how to use in text though, as it is very visual.
Type 'guide' into the command window and create a blank GUI to get started.
From the new page you can drag and drop icons on the the back drop, all of which will show up when you run your gui. You can add panels, buttons, sliders, axes and whatever you want from here.
Some buttons will have automatically generated callback functions. (When you save the guide figure, it will automatically generate an .m file for you to edit that goes with it)
If the slider does not have an automatic call back, if you edit the properties you can click the button next to a property called "CallbackFunction" or something like that to get one.
Inside the sliders callback you can tell it do things anytime the slider is changed. What you probably want to do is 'get' the value of the slider so you can use it in your program/functions. I believe you can get it with this line of code inside your slider callback
slider_val = get(hObject, 'value')
The variable slider_val can now be used and passed around at your discretion.
Thats the general idea, but its hard to explain guide in just text! :O

Categorías

Más información sobre Migrate GUIDE Apps en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by