MATLAB: How to share multiple variables in different m files

MATLABshare multiple variables

Now i have two solutions to address the problem.
  1. The one is set all the variables need to share to ouptput variable,and input them when it's used in other function.
  2. The other is set all these variables to global variable.Does Matlab can address it by other better solution?

Best Answer

If your m-files are scripts running independently of each other, my choice would be to use a .mat file. Save all the variables in a .mat file and then using load with an output argument, load only the variables needed by a particular script into the workspace.
If your m-files are all functions running in your workspace at the same time, you can pass the variables as arguments as needed by the functions and then return new values for them as function outputs (or use the much less-desirable option of declaring them as global variables).
It all depends on what your m-files are doing, and whether they need the variables serially or simultaneously.