Using a saved structure containing data again and again during model runs
1 次查看(过去 30 天)
显示 更早的评论
I have a structure that stores the thermodynamic properties of different fluids. The fields of the strucutre correspond to different properties some of which are single values and some of them are numeric arrays. I use this struture during modeling of different process units to calculate fluid properties. So, before I start running the models, I first load this stucture in the workspace and then make it a global variable so that all the functions have access to this variable. Another way I have tried is to load this structure inside a class and store it in a class property. Then I can pass the object to different functions.
In both these options, I have to load the structure first either in the workspace or inside a class. I am not sure if this is the correct way. I dont like the global variable approach. And I think that loading it inside a class might not be efficient as everytime a new instance of the object is passed to a function it will load the structure again. Perhaps I am missing something very obvious.
Some pointers towards better way of doing this would be appreciated.
1 个评论
Steven H
2023-5-15
编辑:Steven H
2023-5-15
Make the class a subclass of the handle class. When you pass the class object to a function, you're not making a copy of the object but you're just passing along a reference to the object.
This is not always immediately clear to everyone so to be sure; imagine we have a handle subclass called Data which has a property Value. I create an instance data = Data; and make a copy of the reference, so dataCopy = data;. Now, if I set data.Value = 1;, you'll see that dataCopy.Value is also updated.
回答(1 个)
Amith
2024-8-16
Hi Abhijay,
In MATLAB, you could consider creating a persistent variable to store your thermodynamic properties structure. A persistent variable is a variable that retains its value in memory across multiple function calls within the same MATLAB session, without needing to be passed as an input argument to the function.
Here's an example of how you could implement this:
function properties = get_properties()
persistent prop_struct;
if isempty(prop_struct)
% Load the thermodynamic properties structure here
prop_struct = load('my_properties.mat'); % replace with your own file name
end
properties = prop_struct;
end
In this example, the get_properties function checks if the persistent variable prop_struct has been initialized and loaded with the thermodynamic properties structure yet. If it hasn't, it loads the structure from a file (you can replace this with your own method of loading the structure). The function then returns the structure as an output argument.
By using a persistent variable, you can avoid the use of a global variable, and you only need to load the structure once per MATLAB session. Each time the get_properties function is called after the first time, it simply retrieves the already-loaded structure from the persistent variable.
You can then call the get_properties function in your modeling functions to obtain the thermodynamic properties structure:
function my_model_func(input_args)
properties = get_properties();
% Use properties to perform calculations
end
In this way, you only load the thermodynamic properties structure once per MATLAB session, and your modeling functions can access it without the need to pass it as an input argument.
Hope this helps!
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Import and Analysis 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!