Use of private method and properties from a MATLAB class to a MATLAB function

15 views (last 30 days)
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 Comments
Image Analyst
Image Analyst on 1 Aug 2024
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
  1. 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
  2. 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.
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.
Manikanta Aditya
Manikanta Aditya on 1 Aug 2024
@Image Analyst, That's correct all the points you mentioned. But I saw it can be possible to do this as in C++ we have something like Friend functions which can access the private prop and methods. So wanted to know how in MATLAB.

Sign in to comment.

Answers (1)

Steven Lord
Steven Lord on 1 Aug 2024
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 Comments
Steven Lord
Steven Lord on 2 Aug 2024
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.
Manikanta Aditya
Manikanta Aditya on 2 Aug 2024
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?
Yes, it is correct. I want the functions to be called outside the class.
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.
I will try it out.
Thank you!

Sign in to comment.

Categories

Find more on Specifying Target for Graphics Output in Help Center and File Exchange

Products


Release

R2024a

Community Treasure Hunt

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

Start Hunting!