Access to an object's fields is only permitted within its methods in matlab - matlab

I need to modify the cluster methods of the phytree object in matlab. After modifying it, I change the name to cluster_second. Then, when I run the function, I got the error saying that
Access to an object's fields is only permitted within its methods.
I understand what is going on here. Because I need to use tr.tree, and it is a field of the phytree object which can only be accessed within that phytree object's method. Obviously, cluster_second does not satisfy that condition. Can anyone help me out with this? Many thanks.

If you want your cluster_second method to be a method of phytree, put the file cluster_second.m into the directory \toolbox\bioinfo\bioinfo\#phytree. All phytree objects will then have an additional method cluster_second (as well as the original cluster method).
It's possible that you may also be able to put it into a different folder named #phytree, as long this #phytree folder is on your path. I'm afraid I don't have Bioinformatics Toolbox on my current machine to test that, though.

Related

AnyLogic: Variables from Simulation will not be displayed anymore

I have added some variables in my simulation start page. Now I want to access them from the main class. However, with ((Simulation)getExperiment()).[...] the variables are no longer displayed. When I press option+space only one of about 150 variables is displayed. This one variable is not different from the other variables in the properties.
All variables are on public and I was able to access all of them a few weeks ago. Now not anymore, although I didn't change much in the model. I have already tried restarting. Did I accidentally remove some package or something? Can someone help me here please? I have only noticed the problem now as my model has runtime errors, presumably as the now unrecognized variables are not being initiated.
Pictures:
You should not access variables from experiments on Main. If you need access, you should pass them on as parameters into Main.
If you have hundreds of variables in the experiment, turn them into fields in a single Java class. Pass 1 instance of that Java class into Main as a parameter.
This way, you always have access, it is easy to add more variables, it is easy to change them and you do not need the awkward (and bad) type-casting you currently do

Is it possible to get the type of a variable while computing completion suggestions?

I'm thinking of creating a VSCode extension to help people use a library I am creating. I've looked in 50 places and can only see documentation on getting the plain text of the document in which the completion suggestions are triggered.
My library exposes a function with two parameters, both objects with the same keys, but the first one will be defined already and the 2nd one will be passed in as an object literal. For example
const obj1 = {a:1, b:2};
doLibraryThing(obj1, {a:[], b:[]});
I want to provide code completion, or a snippet, or anything that can detect that doLibraryThing is being called, and know what properties were defined in obj1, even though usually it will be imported from another file; and then use those properties to generate the suggestion with the same properties, {a:[], b:[]}.
Is this possible?

MATLAB doesn't show help for user-created class private methods and properties

This is the problem:
Create a class and set the access to be private for some of the properties or methods.
Use the doc command for the created class. This will auto-generate documentation from your comments and show it in the built-in help browser.
doc classname
The problem is that documentation for the private properties and methods is not shown in the help browser. Is there any way to overcome this problem?
So I spent like 10 minutes using the debugger, jumping from one function to the next, tracing the execution path of a simple doc MyClass call.
Eventually it lead me to the following file:
fullfile(toolboxdir('matlab'),'helptools','+helpUtils','isAccessible.m')
This function is called during the process of generating documentation for a class to determine if the class elements (including methods, properties, and events) are publicly accessible and non-hidden. This information is used later on to "cull" the elements.
So if you are willing to modify MATLAB's internal functions, and you want the docs to always show all methods and properties regardless of their scope, just rewrite the function to say:
function b = isAccessible(classElement, elementKeyword)
b = true;
return
% ... some more code we'll never reach!
end
Of course, don't forget to make a backup of the file in case you changed your mind later :)
(on recent Windows, you'll need to perform this step with administrative privileges)
As a test, take the sample class defined in this page and run doc someClass. The result:
This behaviour is by design - the auto-generated documentation is intended for users of the class, who would only be able to access the public properties and methods.
There's no way that I'm aware of to change this behaviour.
You could try:
Use an alternative system of auto-generating documentation such as this from the MATLAB Central File Exchange (which I believe will document all properties, not just public).
Implement your own doc command. Your doc command should accept exactly the same inputs as the built-in doc command, detect if its inputs correspond to your class/methods/properties etc, and if so display their documentation, otherwise pass its inputs straight through to the built-in doc. Make sure your command is ahead of the built-in on the path.

Do I have to put get/set methods in the class definition in matlab ?

Is one forced to place all get and set functions in the class definition file in Matlab ?
I'm asking since this really makes the file a bit messy and defeats the purpose of having a class definition folder.
Yes, if you use property set and get access methods (in fact any method with a dot in the name), you must include them within the classdef file, not in separate files. See the documentation.
However, if you have have a special reason to want to put as much as possible in separate files, you can define methods getMyProp and setMyProp in separate files, and then within the classdef file have the get.myProp and set.myProp functions call them.
If you use them then you need to define them. but you can also define your variables as public.

Using MATLAB, why does something like fts.data work in one directory but not another?

I am working with the financial tooldbox that has a type called FINTS. If I copy some code out of its toolbox directory to customize it, when I try do do something like fts.data, `I get
The specified field, 'data', does not exist in the object.
But the same thing works fine in the MATLAB library directory. They are both in my path, so what else do I need to change?
I think, but I haven't checked the documentation on this one, that it is a peculiarity of MATLAB that the class FINTS must be defined in the directory #fints. So if you want to extend the class, you have to put your code into that directory. And if you want to work on a class MYFINTS, you need to put the code into directory #myfints.
OK, I figured it out. MATLAB defines class methods in what it calls method directories which are named after the class. So in this case, the class is fints, so all its methods are in #fints. All I had to do was make a new directory in my own workspace called #fints, and it will become another class method of fints. You can see all the methods a class has by calling what className.
Make sure the path is specified from the root directory, and not relative.
For instance
addpath 'c:\...\...\MATLAB\mytoolbox
not
addpath 'mytoolbox'
the latter will break if you change your working directory