I want to write a modular matlab program and I have some data structures such as history in my program. Is that true that I have to keep all my data-structures in the main script of my program? In other words if I have some arrays and fields of data, if I put them in other m files, such as functions, they are temporal and they are going to be collected as garbage in my program execution. I am a java developer and now I want to develop some code in matlab.
-
2@HunterJiang As I know making variables global, causes difficulties in managing the code by the programmer. I want some method to handle data in different classes and files, just like Java.sajad– sajad2018-04-29 09:19:32 +00:00Commented Apr 29, 2018 at 9:19
-
1Just like you should be doing it in Java... use classes and store data into the properties of their instances.Tommaso Belluzzo– Tommaso Belluzzo2018-04-29 12:40:58 +00:00Commented Apr 29, 2018 at 12:40
1 Answer
As Tommaso suggested in a comment, you should use classes. Look at the documentation for classdef to get started. The full documentation to create classes starts at this page.
But to directly answer your question: it is possible to store static data in functions: see persistent.
If you're making a GUI, there are built-in ways to store data, see guidata.
Finally, there is also getappdata and setappdata, which set global variables but specific to one app.
For all that's good in this world, don't use global, it's not worth the hassle, here are plenty of better alternatives.
PS: if the links here break, note that it is always possible to type help <cmd> to get help on one of the functions mentioned here.