storing data from function

조회 수: 14 (최근 30일)
Jonathan
Jonathan 2012년 5월 31일
댓글: nazrin mokhtar 2020년 2월 28일
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
Jonathan
Jonathan 2012년 5월 31일
But when I enter [sound, freq] = wavread(file) into the command line, the variables store in the workspace.

댓글을 달려면 로그인하십시오.

채택된 답변

Oleg Komarov
Oleg Komarov 2012년 5월 31일
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
Jonathan
Jonathan 2012년 5월 31일
I tried that. sound and freq show up in the command line, but not in the workspace.
Jonathan
Jonathan 2012년 5월 31일
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!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Stephen
Stephen 2012년 5월 31일
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
Jonathan
Jonathan 2012년 5월 31일
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 2020년 2월 28일
i have same problem to save in workspace...can i know how to solve it

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Just for fun에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by