How to extract variable from function
显示 更早的评论
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 个评论
Jan
2016-11-14
Now please explain what "a table" is: An XLS file? A tab separated text file? A HTML or LaTeX table? Do you want a screenshat of an uitable or write a table-object in a MAT file?
Rasmus Dueholm
2016-11-14
Guillaume
2016-11-15
"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?
回答(1 个)
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
- 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
- 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
- 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.
类别
在 帮助中心 和 File Exchange 中查找有关 Common Operations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!