Custom code suggestions with functionSignatures.json does not preserve char type with "choices={}"
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I am trying to implement code suggestions with the functionSignatures.json file. I have certain functions which require character input arguments. For one such function in the JSON file I have written:
{
"name": "model",
"kind": "required",
"type": ["char", "choices={'f','n','u'}"],
"purpose": "type of model"
},
However, in the Editor the autocomplete drop down sugggestion offers and then Tab/Enter inserts a string scalar (either "f", "n" or "u") instead of a character.
Is this a bug or am I missing something?
1 commentaire
Markus Leuthold
le 2 Août 2022
if you start you completion sequence with char, Matlab continues completing with char. For example assum the following functionSignature.json
{
"name": "model",
"kind": "namevalue",
"type": ["char", "choices={'f','n','u'}"],
"purpose": "type of model"
},
After you type
fcn('
both the key 'model' and the values 'f','n','u' will be completed in char.
But you're right, the default is string, which is sometimes cumbersome. I normally make sure all my functions accpet both string and char
Réponses (1)
Matt Cooper
le 17 Juin 2022
This is not a bug, it is the expected behavior (assuming I understand your question). To make sure I understand your question, you want to assign to 'model' a character array like:
model = char('f','n','u');
and you want to be able to autocomplete this selection? If so, then you have misunderstood how the json file works.
The line:
"type": ["char", "choices={'f','n','u'}"],
is designed to show the user the three available options ('f', 'n', or 'u') that the value of the variable 'model' is allowed to take. If you had three different models, one named 'f', one named 'n', and one named 'u', then you would have the correct setup.
To get what you want, you could change it to:
"type": ["char", "choices={'fnu','abc','xyz'}"],
where I made up two other options 'abc' and 'xyz'. Then in your code, parse them out like:
modelselection = [model(1); model(2); model(3)];
or however you want.
If you don't care about autocomplete, then just remove the part with "choices" and parse out the input however you want. However, this is actually a neat trick to getting autocomplete options for multiple parameters all at once. Normally you would need a separate variable for whatever 'f', 'n', and 'u' represent, but this way you get all three at once, albeit in a cryptic manner that nobody else will understand unless they are familiar with the options.
There is another option you might look at to pass in a structure but it can be tricky to figure out (each field of the structure you pass in is treated as a namevalue pair but must have a corresponding addParameter definition in the input parser scheme which is quite rigid but leads to more compact input notation).
2 commentaires
Matt Cooper
le 22 Juin 2022
In that case I suggest including 'string' as a valid type in your validateattributes or properties block at the top of your function and then just convert to char. I confirmed the same behavior with my functions but I never noticed this because I must reflexively type the single quote character whenever I want the list of choices (which are displayed as chars if that's how they're defined in the json), but as you note, if you press the tab key without first typing the single quote, you will get a list of strings. So, just accept either strings or chars (no need to edit the json, just the input parser validation block if you are using that) and then convert to char.
Voir également
Catégories
En savoir plus sur JSON Format dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!