Import data from file inside "classdef properties"

35 ビュー (過去 30 日間)
slow_hand
slow_hand 2024 年 3 月 12 日
コメント済み: slow_hand 2024 年 3 月 30 日 11:57
Hi all,
I'd like to understand how to do something that i find really intriguing.
I'm talking about importing data from an external file (Excel) and then assigning specific fields' values (from that file) to the variables defined within the "properties" of a "classdef" in my code. These variables are useful in different parts of the code, so that if I change the external file I can change the properties mentioned, which are automatically called inside the code itself.
I've already defined the parser for the external file, but I do not know how to integrate it into the classdef.
Your help would be greatly appreciated.
Thanks!
  5 件のコメント
Steven Lord
Steven Lord 2024 年 3 月 13 日
Ideally, each time I run the code
What do you mean by "the code"?
  • The class constructor?
  • Each class method?
  • Each file that accesses a property of the object?
What's the purpose of these properties? How do they get used in your workflow? Knowing a little more about your planned usage may help understand if there's an alternate approach that may be more efficient than your current planned approach. This may help avoid the XY problem.
slow_hand
slow_hand 2024 年 3 月 15 日
To Steven: For "code" I mean exactly each file that accesses a property of the object.
For now, in the classdef, I've defined a function in methods that parses the Excel file and extracts certain properties/value/fields.
Then, in the properties section of the class, I recall these properties by writing:
propertyName = classdefName.functionName.propertyName
Where propertyName is the property extracted from the Excel, classdefName is the name assigned to the classdef and functionName is the function mentioned before.
It seems to work, however do you think it is a valid approach??
Thanks.

サインインしてコメントする。

採用された回答

SAI SRUJAN
SAI SRUJAN 2024 年 3 月 28 日 5:13
Hi,
I understand that you are facing an issue with importing data from a file to inside of 'classdef properties'.
This can be done by using the class constructor or a dedicated method to load the data when an instance of the class is created or when needed.
Below is an example of how you might structure your class to include importing data from an Excel file and assigning specific fields' values to the class properties.
classdef MyClass
properties
Property1
Property2
end
methods
function obj = MyClass(excelFilePath)
if nargin > 0
obj = obj.loadExcelData(excelFilePath);
end
end
function obj = loadExcelData(obj, excelFilePath)
data = readtable(excelFilePath);
obj.Property1 = data.Field1;
obj.Property2 = data.Field2;
end
end
end
I hope this helps!
  1 件のコメント
slow_hand
slow_hand 2024 年 3 月 30 日 11:57
Thank you SAI, this is exactly what I was meaning in my answer above!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeArgument Definitions についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by