- You can use 'whos' command in the function to watch all local variables. The reason why local variables of the function are visible after its execution is, you are using step by step debugging(using break point).
- Also, the reason for k = 0 is because you are using 'class' keyword in the 'exist()' command. This will compare the given file to classes. Instead of 'class', you can use 'file' keyword. You have to give full file name with extension and path.
- You can use 'audioread' function to read '*.wav' files. For more information refer this link.https://in.mathworks.com/help/matlab/ref/audioread.html
Using open() in a function doesn't work as intended
4 次查看(过去 30 天)
显示 更早的评论
Hello,
this may be a profane mistake or problem...
I want to open a .wav file. In order to do that, I use
open()
of course. It has always worked for me. As I have a dynamic directory from which I get the .wav files, I also specify the directory.
But when I want to do that in a function, strangely the file doesn't seem to be opened in the "local" workspace within the function, but instead in the "global" workspace (sorry to call it that way but I don't know a better name) that gets displayed outside of the function, and it seems to be done after the function has been executed.
That causes my function to fail of course, as the desired file has not been loaded and the function can't work with the data.
When I go step by step in debug-mode, nothing happens when going over the line of code. Here is a testfunction that I built for this purpose:
function k = testfunction(subjectname)
open(['..\' subjectname '\SpeakerAudio.wav']);
open(['..\' subjectname '\MicrophoneAudio.wav']);
csv = csvread(['..\' subjectname '\csvdata.csv'],1,0);
if exist ('SpeakerAudio','class')
k = 1;
else
k = 0;
end
end
when using the Testfunction, k is always 0.
The Data gets opened AFTER executing the function and is present in the workspace. Can someone explain this?
Am I doing something wrong?
I am using Matlab R2014b if anyone is wondering
Thank you,
Michael
0 个评论
回答(1 个)
Niwin Anto
2018-2-20
2 个评论
Niwin Anto
2018-2-21
It is working fine on my end. I am using MATLAB R2017b. You can try 'whos' command to confirm, where the variables are loaded(function local variable would not visible in workspace window).
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Environment and Settings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!