- 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
2 views (last 30 days)
Show older comments
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 Comments
Answers (1)
Niwin Anto
on 20 Feb 2018
2 Comments
Niwin Anto
on 21 Feb 2018
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).
See Also
Categories
Find more on Whos 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!