MATLAB: Can’t I concatenate input variable to file extentsion in another variable

errorfilestrings

I am trying to take the string input fname and create a new file 'fname.aif' but the variable faif is not being recognized by Matlab ( Error: Undefined function or variable ). Can someone explain why? My simple code is for generating a single tone is attached. The goal is to be able to convert the generated tone to aiff format. I need to be able to input '690Hz' as fname and have a new .aif file created.Screen Shot 2019-04-25 at 10.11.29 AM.png

Best Answer

Since you do not return the variable named faif from the function, once the function call ends it is destroyed along with the rest of the function's workspace.
Do you want to write Tone_with_silence to the file with that name? If so, it's easiest to do that inside the function.
Do you want to use the filename stored in faif (and possibly the Tone_with_silence) variable outside this function? If so, declare the function to return them as output arguments and call the function with output arguments.
See this documentation page for basic information about function workspaces.
Note that both declaring the function's output arguments in its definition and calling it with output arguments are necessary. MATLAB doesn't "magically" put the output arguments you declare the function to return in its caller's workspace. I mention this only because I've seen people in the past assume that behavior.
Related Question