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

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 Kommentare

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.
@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.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Steven Lord
Steven Lord am 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 Kommentare

Hi Steven, so here my reason for breaking the code is due to the increasing complexity. So I make new function files, now I have to use those function files which I separated, but the properties are been defined in main class with private access, now I feel it’s so complex to change all, rather than that I wanted to know if there is a way to access rather than changing the functions or the main class file
Hi Steven, so here my reason for breaking the code is due to the increasing complexity.
This is reasonable.
So I make new function files, now I have to use those function files which I separated,
The code in those new function files, if they were "spun off" from other function files (as opposed to class methods defined in separate function files), would have the same access to class methods and properties as they did before. So did you extract those functions' code from other function files or from class methods?
but the properties are been defined in main class with private access,
And why do those functions need access to the class's PRIVATE information? If the class author wanted anybody to have access to that information or those actions, they wouldn't have made them private.
now I feel it’s so complex to change all, rather than that I wanted to know if there is a way to access rather than changing the functions or the main class file
You've told us what you're trying to do but not why. Why should these functions be entitled to access the class's private information? "Because it's easy" is one potential reason, but that would represent a violation of encapsulation and I would recommend avoiding that.
If these aren't standalone functions but are methods of the class defined in separate function files, as long as the class and those methods defined in separate function files are in a directory whose name starts with the at-symbol @ followed by the name of the class, they'll be methods. If that's the case you could use the property and method attributes of either their class or the class whose methods/properties they're trying to access to control whether or not they're accessible.
Very much thanks for your detailed reply back.
And why do those functions need access to the class's PRIVATE information? If the class author wanted anybody to have access to that information or those actions, they wouldn't have made them private.
Here, I will give clarity, there is a class file, I have developed, it had all functions related to the class, so my class job here is to read two different type of file format data. So, properties methods I defined are in single class file as the two functions which read the two formats are within the same class file. Now as to reduce compelxity and in future, If I want to add more or do enhancements, it will be better if I have two seperate function files which read two formats and the commoninfo will be in main class file, but reading and parsing related will be in the two functions. So this is the reason to seperate, class's private info was part and needed earlier to these functions as they are in same class, now as I seperate there is an issue.
You've told us what you're trying to do but not why. Why should these functions be entitled to access the class's private information? "Because it's easy" is one potential reason, but that would represent a violation of encapsulation and I would recommend avoiding that.
I have answered this questions I think in above reply.
Thanks again for your time.
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.
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!

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Construct and Work with Object Arrays finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2024a

Gefragt:

am 1 Aug. 2024

Community Treasure Hunt

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

Start Hunting!

Translated by