开发者

Tab-completion of filenames as arguments for MATLAB scripts

开发者 https://www.devze.com 2022-12-13 00:39 出处:网络
We all know MATLAB provides tab-completion for filenames used as arguments in MATLAB function like importdata开发者_开发知识库,imread. How do we do that for the functions we create?

We all know MATLAB provides tab-completion for filenames used as arguments in MATLAB function like importdata开发者_开发知识库,imread. How do we do that for the functions we create?

EDIT:

Tab-completion of filenames as arguments for MATLAB scripts

Displays the files and folders in the current directory.


Caution: unsupported hack here.

Take a look at the file toolbox\local\TC.xml in your Matlab installation. This appears to contain the argument type mappings for tab completion. (I found this by just grepping the Matlab installation for "imread" in R2009b.)

Adding this line inside the <TC> element will get you tab-completion of file names for each of its arguments.

<binding name="importdata"        ctype="FILE"/>

This requires modifying the Matlab installation in place. For some of the other files in toolbox/local, like classpath.txt, you can override them by placing modified copies in the directory you start Matlab from, but this didn't work for TC.xml for me.


There is no supported way to add your functions to the argument Tab completion, but one trick I use is to put a "!" in front of the command so it is treated like a system command. Tab will then complete file paths. Once my MATLAB command is done, I home to the beginning of the line, delete the "!" and press enter.


For Matlab 2016a and above :

The file Tc.xml is not present in Matlab 2016a onwards. It uses a .json (Java Script Object Notation) file to achieve the same. A simple example of this can be as follows.

Suppose you have a Matlab function file called myFunction.m. Furthermore, suppose that this function needs files with extension .ext as input and you want the tab-completion to show all possible input options for this function. Then, write the following content in a file and name it functionSignatures.json.

{
"myFunction":
{
  "inputs":
  [
    {"name":"filename", "kind":"required", "type":"filepath=*.ext" }
  ]
}
}

Place this file in the same directory as myFunction.m file. Now, restart Matlab.

What this file does : While typing in the function input, upon pressing tab, you will see a list of files with the extension .ext popping up. If you want all the files to be shown in tab completion popup irrespective of their extension, then replace "type":"filepath=*.ext" with "type":"filepath" in the file functionSignatures.json.

Source : https://www.mathworks.com/matlabcentral/answers/306157-how-to-configure-tab-completion-for-my-function-in-matlab-2016#answer_237857

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号