functionSignatures.json : specify filepath

35 vues (au cours des 30 derniers jours)
covfefe
covfefe il y a environ 15 heures
Modifié(e) : Stephen23 il y a environ 2 heures
I like the idea of Matlab function signatures.
How can I specify the file path to search in?
For example, I have a function LoadInputFile , and I want this to default to the directory C://Folder1/Folder2/Folder3, how do I do so? Can I do so both absolute and relative paths?
I have tried a lot of different attempts, all unfortunately failed.
  • keywords type / basePath , both failed
  • absolute, relative paths
  • forward or backward slashes
  • single or double slashes
Thank you
"LoadInputFile": {
"_description": "Load a text input file",
"inputs": [
{
"name": "filename",
"kind": "optional",
"type": "filepath=Folder2/Folder3/*.txt",
"basePath": "C://Folder1/Folder2/Folder3/",
"purpose": "some file is now loaded"
}
]
},

Réponses (1)

Stephen23
Stephen23 il y a environ 13 heures
Modifié(e) : Stephen23 il y a environ 13 heures
I suspect that you have a slight misconception of what code suggestions and completions are capable of.
"How can I specify the file path to search in?"
Code suggestions and completions do not really "search" for anything, so your intent here is unclear.
"I have a function LoadInputFile , and I want this to default to the directory C://Folder1/Folder2/Folder3, how do I do so?""
To specify a drop-down value that the user can select you can specify the CHOICES value of the TYPE property:
{
"LoadInputFile": {
"inputs": [
{
"name": "filename",
"kind": "optional",
"type": ["char", "choices={'C://Folder1/Folder2/Folder3/'}"],
"purpose": "some file is now loaded"
}
]
}
}
It is shown to the user in the code suggestions drop-down menu:
Which after being accepted looks like this:
Note that this value is just a suggestion (not a restriction), i.e. the user can modify it to anything they would like.
"Can I do so both absolute and relative paths?"
Of course, you can use any path style that your function was written to accept: code suggestions themselves do not check the class of the input, do not check the existence of a path, or that it is a valid path, or anything else of that ilk. It simply provides some text as an input to your function. Therefore it is your function that needs to check that the path is valid!
To actually check if the input text represents a valid path and to provide a default value use an ARGUMENTS block (or one of its historic equivalents):
  3 commentaires
covfefe
covfefe il y a environ 4 heures
@cov@Stephen23, thank you for the prompt and detailed answer.
With your changes, I can now fill a hardcoded foldername: C://Folder1/Folder2/Folder3.
However, what I am looking for is the list of *.txt files in this folder (not the foldername).
Adding a *txt filter just changes the string:
"LoadTextFile": {
"inputs": [
{
"name": "filename",
"kind": "optional",
"type": ["char", "choices={'C://Folder1/*.txt'}"],
"purpose": "some file is now loaded"
}
]
}
Instead, if I use filepath, then I can get the file list (but only in the current directory, not in C://Folder1)
"type": "filepath=*.txt",
Maybe I need a combination of both, something along the lines of this?
"type": ["filepath", "choices={'C://Folder1/*.txt'}"],
Stephen23
Stephen23 il y a environ 3 heures
Modifié(e) : Stephen23 il y a environ 2 heures
"However, what I am looking for is the list of *.txt files in this folder (not the foldername)."
You can do that too. Write a small function that collects the information that you require and returns a cell array of character vectors. For example:
{
"LoadInputFile": {
"inputs": [
{
"name": "filename",
"kind": "optional",
"type": ["char", "choices=myfiles('./*.m')"],
"purpose": "path of the file to load"
}
]
}
}
where
function C = myfiles(P)
S = dir(P);
C = fullfile({S.folder},{S.name});
end
As far as I can tell, the FILE value only searches in the current folder. See also:

Connectez-vous pour commenter.

Catégories

En savoir plus sur Environment and Settings dans Help Center et File Exchange

Produits


Version

R2025a

Community Treasure Hunt

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

Start Hunting!

Translated by