- instantiate the class as a new object (class variable) and try to call some of the methods that are private to the class file, or
- the class is a static class and you are trying to call the private functions in the class directly from one or both of the two new function m-files.
Use of private method and properties from a MATLAB class to a MATLAB function
8 次查看(过去 30 天)
显示 更早的评论
I have a MATLAB file and there are couple of functions defined, I split two of the functions into new function files and invoked them in the main class file, so now the issue is there are some methods functions and properties which are set to private, so because of this the new function files arent able to get those methods and properties, the option I had is to keep Access/Set Access to public, but that is not an expectation here,
So, can anyone help or suggest here without making access change how to fix this issue, Any help is highly appreciated.
2 个评论
Image Analyst
2024-8-1
So what I'm getting from this is you had one file that had two functions in it and you now separated them into two new function files. And you have another file that is a class file, which has methods and properties but some of those methods and properties are private and some are public. Right so far?
And you are saying that the functions in the two new function files either
Is all that correct? If so, why do you think that some external function should be able to call a function that you explicitly made private to that class file alone? Only methods within that class file will be able to call any methods that were made private. Other functions and files will not be able to see those private functions, of course.
回答(1 个)
Steven Lord
2024-8-1
You can grant classes access to otherwise-private properties using the Access, GetAccess, and/or SetAccess property attributes and similar for methods with the Access method attribute. You cannot as far as I'm aware grant functions access to those restricted-access properties and methods.
Why are you trying to give plain old functions access to those private or protected properties or methods? Usually if the class author didn't want to let any chunk of code access that data or those operations they have a reason why they added that restriction. If we know what you're trying to do we may be able to offer some suggestions for how to achieve your goal (or explain why we think what you're trying to do is a code smell.)
5 个评论
Steven Lord
2024-8-2
So to make sure I understand, you're trying to create helper functions that multiple methods in your class can call. Is that correct? If so do you want those helpers to be callable by code outside the class itself or only by code inside the class?
If you only need these helpers inside the class, rather than making them separate functions I'd probably make them private methods of the class or maybe a local function inside the class file. As a method they'd have access to the properties and methods of the class. If you have a local function, it should probably have a specific enough purpose (a Single Responsibility) that the methods calling it should pass the values of the necessary properties into it as individual variables rather than passing the whole object. So instead of passing obj into the local function and having that local function retrieve obj.privateproperty, have the method retrieve x = obj.privateproperty and pass x into the local function.
If you do need the helpers to be callable by other code outside the class itself, I'd would treat it the same way as the local function I mentioned above. They should have a single responsibility and so not get the whole object, just the values that they absolutely need to satisfy their purpose.
To use an analogy, both my doctor and my tax preparer need some of my private information. But I don't tell my doctor how much money I make, nor do I tell my tax preparer how much I weigh. They get only the information they have need to know.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Specifying Target for Graphics Output 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!