Borrar filtros
Borrar filtros

Using open() in a function doesn't work as intended

4 visualizaciones (últimos 30 días)
Michael Göken
Michael Göken el 6 de Feb. de 2018
Comentada: Niwin Anto el 21 de Feb. de 2018
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

Respuestas (1)

Niwin Anto
Niwin Anto el 20 de Feb. de 2018
  • 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
  2 comentarios
Michael Göken
Michael Göken el 20 de Feb. de 2018
Thanks for adressing my problem.
  • I am aware that when using debug mode, the functions internal variables will be visible when going step by step. But the variables that I want to load with open() don't get loaded into the functions internal variables at all. They instead get loaded into the global workspace AFTER executing the function. When loading the .csv data also included in the example, everything works fine.
  • As I am using the ITA-Audio-toolbox, my .wav files will be loaded into the workspace as an object of the itaAudio-class, I should have mentioned that. Sorry
  • audioread() only loads the raw sound data without any meta-data like sampling rate or else as it doesn't load it as an object of the itaAudio class. I need that data, so I don't really have an option but to use open()
Niwin Anto
Niwin Anto el 21 de Feb. de 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).

Iniciar sesión para comentar.

Categorías

Más información sobre Environment and Settings en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by