how do I change the default open behavior to not be a class property

16 views (last 30 days)
How do I change the default behavior in the MATLAB editor so that selecting text and opening it (selecting "open") doesn't go the property first (as opposed to a class with the same path)? (note, this may also apply to functions in the same package, I haven't tested it
Consider the following layout:
+p/@class1
+p/+class1/@class2
If class1 has a property "class2" then if I select code that is "p.class1.class2" and open it the editor goes to the property "class2" instead of the class "class2"

Accepted Answer

Deepak
Deepak on 27 Mar 2025
In MATLAB, the behavior of resolving names when selecting and opening code is indeed governed by its internal name resolution rules. When you have a property and a class (or function) with the same name, MATLAB defaults to opening the property first. Unfortunately, there is no direct, documented way to change this behavior. However, you can consider the following workarounds:
1. Manual Navigation:
  • Instead of relying on the "open" command, manually navigate to the desired class or function in the Current Folder window or use the "Go To" feature in the editor to find the class or function definition.
2. Use Fully Qualified Names:
  • When writing code, use fully qualified names to distinguish between the property and the class. This can help clarify your intentions and make it easier to navigate manually.
3. Refactor Code:
  • If feasible, consider renaming either the property or the class to avoid name conflicts. This can improve code readability and reduce confusion.
4. Create Helper Scripts:
  • Write a small script or function that uses which or similar commands to programmatically determine and open the desired class or function file.
Below is a sample MATLAB script that helps to open a specific class or function:
function openClassOrFunction(name, type)
% openClassOrFunction Opens a class or function file by name.
%
% Usage:
% openClassOrFunction('className', 'class')
% openClassOrFunction('functionName', 'function')
if nargin < 2
error('Please specify both the name and type (class/function).');
end
switch type
case 'class'
% Use the 'all' option in which to find the class file
filePath = which(name, '-all');
case 'function'
% Use the 'function' option in which to find the function file
filePath = which(name);
otherwise
error('Type must be either "class" or "function".');
end
% Check if filePath is a cell array and extract the path if needed
if iscell(filePath)
filePath = filePath{1};
end
if isempty(filePath)
error('The specified %s "%s" could not be found.', type, name);
else
% Open the file in the MATLAB editor
edit(filePath);
end
end
Please find attached the documentation of functions used for reference:
I hope this helps in resolving the issue.

More Answers (0)

Categories

Find more on Startup and Shutdown 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!