storing data from function

14 visualizaciones (últimos 30 días)
Jonathan
Jonathan el 31 de Mayo de 2012
Comentada: nazrin mokhtar el 28 de Feb. de 2020
Hi,
I have this code in my function file: [sound, freq] = wavread(file) and for some reason sound and freq are not being stored in the workspace. What am I doing wrong?
  1 comentario
Jonathan
Jonathan el 31 de Mayo de 2012
But when I enter [sound, freq] = wavread(file) into the command line, the variables store in the workspace.

Iniciar sesión para comentar.

Respuesta aceptada

Oleg Komarov
Oleg Komarov el 31 de Mayo de 2012
If you're using wavread inside a function then you have to return them to your base workspace explicitly because the workspace of a function is kept separate.
Example, will not return sound and freq to the base workspace:
function foo(file)
[sound, freq] = wavread(file)
end
You have to declare the outputs explicitly:
function [sound, freq] = foo(file)
[sound, freq] = wavread(file)
end
and call it as
[out1,out2] = foo(file)
  2 comentarios
Jonathan
Jonathan el 31 de Mayo de 2012
I tried that. sound and freq show up in the command line, but not in the workspace.
Jonathan
Jonathan el 31 de Mayo de 2012
nevermind, I had a bit of code at the very end that wasn't defined so the function wasn't finishing. Thanks for the help!

Iniciar sesión para comentar.

Más respuestas (1)

Stephen
Stephen el 31 de Mayo de 2012
So, you have:
function something
[sound, freq] = wavread(file);
end
? If so, you need to output those values like
function [sound, freq] = something
then when you call the function, it will spit out those values. Otherwise, they only exist in the temporary workspace of the function, which gets cleared out when it's done
  3 comentarios
Jonathan
Jonathan el 31 de Mayo de 2012
nevermind, I had a bit of code at the very end that wasn't defined so the function wasn't finishing. Thanks for the help!
nazrin mokhtar
nazrin mokhtar el 28 de Feb. de 2020
i have same problem to save in workspace...can i know how to solve it

Iniciar sesión para comentar.

Categorías

Más información sobre Just for fun en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by