How to extract variable from function

7 visualizaciones (últimos 30 días)
Rasmus Dueholm
Rasmus Dueholm el 14 de Nov. de 2016
Comentada: Guillaume el 15 de Nov. de 2016
Hi all. I am relatively new to MATLAB and am facing some difficulties that I hope someone can help with. I want to export a file (a table (tab separated file)) from MATLAB, but since I am only using functions I can't figure out a way to do it. I have attached the files used
I hope someone can help me out - Rasmus
  3 comentarios
Rasmus Dueholm
Rasmus Dueholm el 14 de Nov. de 2016
Sorry. A tab separated text file will be prefered, but an XLS could also be used.
Guillaume
Guillaume el 15 de Nov. de 2016
"since I am only using functions". Actually, you're also using a script that you're calling from each of your function to initialise some variables. In term of programming practice, this is almost as bad as using global variables.
Anyway, exporting data is easy, but what exactly do you want to export?

Iniciar sesión para comentar.

Respuestas (1)

dpb
dpb el 14 de Nov. de 2016
Editada: dpb el 15 de Nov. de 2016
dlmwrite('yourDesiredOutputFilename.dat',MatlabVariableToWrite,\t);
See
doc dlmwrite % for details/examples, additional optional parameters such as precision...
Also try
help iofun
and look through the list of file i/o function high-level descriptions to see the (veritable plethora) of options from which to choose. Pick from the list what seems most appropriate for your purpose.
I'd strongly recommend working through the "Getting Started" section in the documentation to become familiar with Matlab basics...
ADDENDUM
OK, hadn't looked at the m-files before; the solution is to either
  1. make the HotWaterTank function (which, btw, is not named via the Matlab convention of naming the m-file the same as the function name at list as posted) a script so the variables will all be in the workplace, or
  2. add the necessary function call to write desired output variables into the HotWaterTank function (which, btw, is not named via the Matlab convention of naming the m-file the same as the function name at list as posted), or
  3. modify HotWaterTank to return the desired variables to the calling routine (or workspace if call from there, of course).
1) is simplest but has the disadvantage of everything then becoming public which is undoubtedly why the originator did as did, while
2) is cleaner in regard to 1) needs more mods to the code as is including defining a file name from user, whether to write or not, etc., etc., etc., ..., while
3) is probably bestest solution--can either return variables as desired or not depending on how call the function and then do whatever with them as desired.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by