Info
此问题已关闭。 请重新打开它进行编辑或回答。
I need to reach datas on workspace
2 次查看(过去 30 天)
显示 更早的评论
I have a listbox and I can add datas names to listbox and datas are sound files
I want to select an item on listbox and play it when I clicked a pushbutton.
15 个评论
Adam Danz
2019-6-3
Is this in app designer / guide / or a custom GUI?
You'll need to add a callback function to your pushbutton. Within the callback function you'll need to access the list box and the current selection. Then you can use audioread() and sound() to read the sound file and play its content.
Emre Akinci
2019-6-3
it is a custom gui
when I tried audioread() or sound() I am getting "argument" or "audio must be real or floating" error.
I am very amateur about guis
thank you for answer by the way
Geoff Hayes
2019-6-3
Emre - please copy and paste your code to this question so that we can get an idea of what you have written. Also, please copy and paste the full error message.
Emre Akinci
2019-6-4

I want to list my workspace items on listbox1. Items are wave sound files. When i added items to listbox1 I want to listen them by pushing listen_button
Adam Danz
2019-6-4
编辑:Adam Danz
2019-6-4
"when I tried audioread() or sound() I am getting "argument" or "audio must be real or floating" error."
What we need is
- the copy-pasted code where you tried audioread() and sound() (not a screen shot)
- The full copy-pasted error message
We might need to audio file too but let's just start with those two things.
Geoff Hayes
2019-6-4
emre - by I want to list my workspace items on listbox1 do you mean that you want to list some files from a particular directory/folder from your computer? Does the user get to choose the folder or are you hard-coding the path to these audio files?
Emre Akinci
2019-6-4
Geoff Hayes - yes i mean that. User get choose files
that codes refreshing listbox1
and codes that starting with function is listen button I'm choosing item from listbox1.
It was easy when i did on command window but im new in gui design so what is wrong?
vars = evalin('base','who');
set(handles.listbox1,'String',vars);
--------------------------------------
function listenButton_Callback(hObject, eventdata, handles)
audio1 = audioread(get(handles.listbox1,'Value'));
sound(audio1,44100);
Rik
2019-6-4
Not having read the whole thread I have two pieces of advice:
1 avoid evalin. You shouldn't need it. It looks like you want a function similar to dir.
2 the Value and String properties are separate. The way you retrieve the Value property looks like you actually mean to select the selected string. Check the code below.
function listenButton_Callback(hObject, eventdata, handles)
val=get(hObject,'Value');str=get(hObject,'String');
filename=str{val};
audio1 = audioread(filename);
sound(audio1,44100);
end
Emre Akinci
2019-6-4
No appropriate method, property, or field
'listbox1' for class
'matlab.ui.control.UIControl'.
I got this error when i tried with hObject.listbox1
The filename specified was not found in the
MATLAB path.
this is second error that i got when i use with handles.listbox1
another advice? or what i did wrong?
Rik
2019-6-4
hObject is already the handle to the object, no need to further edit that part of my code.
If you're using that syntax anyway you can also use the following code (which is equivalent to what I wrote in my prior comment).
function listenButton_Callback(hObject, eventdata, handles)
filename=hObject.String{hObject.Value}
audio1 = audioread(filename);
sound(audio1,44100);
end
Emre Akinci
2019-6-5
Rik I added your codes without edit
and I got this
Cell contents reference from a non-cell array
object.
Rik
2019-6-5
I missed the point that you were using this function as a callback to a button, not the listbox itself. So in that case you do need to specifically load the String and Value properties from the listbox instead of the button.
% this assumes handles.listbox1 is a handle to the listbox
% (which it is if you're using GUIDE to create this GUI)
function listenButton_Callback(hObject, eventdata, handles)
val=get(handles.listbox1,'Value');str=get(handles.listbox1,'String');
filename=str{val};
audio1 = audioread(filename);
sound(audio1,44100);
end
Emre Akinci
2019-6-5
I am adding datas with manually,
I'm getting this error
The filename specified was not found in the MATLAB path.
and thats code when i try to list items in listbox
something is wrong with it?
vars = evalin('base','who');
set(handles.listbox1,'String',vars);
Rik
2019-6-5
You shouldn't be using evalin. You should probably be using one or more of these functions: uigetdir, dir, and uigetfile.
Emre Akinci
2019-6-5
thanks Rik!
I am very happy with that.
How can I thank you? :D
There is my working codes
function listenButton_Callback(hObject, eventdata, handles)
audio = get(handles.listbox1,'String');
file = get(handles.listbox1, 'Value');
audio1 = audioread(audio{file});
sound(audio1,fs); % You can set fs by manual etc.:sound(audio1,44100);
function getFileButton_Callback(hObject, eventdata, handles)
% hObject handle to yenileButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
sounds = uigetfile('*.wav','Choose one or more','MultiSelect', 'on');
set(handles.listbox1,'String',sounds);
此问题已关闭。
回答(0 个)
此问题已关闭。
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
