Without your function's code, it's a bit of a guess, but I think your issue probably has to do with variable workspaces (other languages call this the variable's 'scope')
Basically, when you are in a script, any variable you create is in the 'base' workspace and is available at the command window and to any other script file.
You can think of script files as just executing each line of code sequentially in the Command Window.
Functions have their own variable workspace (scope). Code executing inside a function can not see variables in the base workspace, or in any other function's workspace.
There are some exceptions to this, and more details. To understand variable scoping/workspaces, I recommend the following:
Those will help you understand what variables are available in your functions. You will have to pass your variable between functions as an argument, or store it in something like appdata (see setappdata and getappdata).
If you are building a GUI using guide, this is what the handles structure is. You can add a field to handles, update the structure ( guidata(hObject, handles); ), and then access it from any callback that has been passed handles as an argument.
Hopefully that wasn't too confusing and gets you pointed in the right direction.