iscellstr warning "To support string in addition to cellstr, include a call to 'isstring'" unavoidable?

5 visualizzazioni (ultimi 30 giorni)
When using the iscellstr function MATLAB always generates a warning "To support string in addition to cellstr, include a call to 'isstring'". Is there any way to avoid this warning besides supressing it with %#ok? Is there an alternate pattern i can use to avoid this warning?
  2 Commenti
Hitesh
Hitesh il 11 Ott 2024
Can you specify the version of MATLAB where you're experiencing this warning? Additionally, could you provide a sample code snippet that reproduces the warning?
Max Heimann
Max Heimann il 11 Ott 2024
I think i selected it when submitting this question, but i not sure where its shown. Im using R2023b.
As for the example its as simple as calling the function.
iscellstr(myVar)

Accedi per commentare.

Risposta accettata

Steven Lord
Steven Lord il 14 Ott 2024
I am just wondering why a function cannot be used without throwing a warning.
Does the function issue a warning when you run it, or does Code Analyzer flag its usage in the Editor? Those are two different things. Based on your comment about %#ok I'm guessing the latter.
Code Analyzer flags with an orange message situations that it can identify where what you're doing is syntactically valid but you may be able to do what you're trying to do more efficiently, or where we can identify that what you're doing is not necessarily what you want to do. As an example, when people say "0 < x < 1" we know what they're trying to do. But that's not what they're actually doing. So Code Analyzer flags that pattern with an orange message. [This is different from a red Code Analyzer message, where what you're doing isn't going to work. It's either not syntactically valid MATLAB code or we're almost certain that you're not doing what you think you're doing. Flagging ++x (which does not increment x) is one example, as is making a class both Abstract and Sealed. In that latter case you must subclass the class in order to use it, but you cannot subclass it.]
Looking in the Code Analyzer preferences, the message that you want to suppress is in the "Suggested Improvements" section.
I suppose if you wanted to suppress this message across your organization you could have the administrators of your license configure Code Analyzer to make this message no longer be enabled in the Editor.
I would probably modify the code to ask iscellstr(x) || isstring(x) to allow my code to support both cellstr and strings. Many/most/all functions in MATLAB that support cellstr inputs also support string inputs.

Più risposte (1)

Harald
Harald il 11 Ott 2024
Hi,
you can suppress this Code Analyzer warning in all files by right-clicking it and selecting "Suppress Message..." > In all files.
Please note that this will suppress the warning only for you and not for any colleagues. Also note that if you don't know the name of a warning anymore, you may have a hard time figuring out how to turn it back on.
If your application also works with string arrays such as ["abc", "def"], you might of course want to switch to isstring.
Also, I am wondering about the broader "why" behind using this function. For validating input arguments of a function, you may find arguments blocks a great alternative. A brief explanation of the idea: MATLAB will automatically check for data types and dimensions and perform conversions if possible. If conversions are not possible, it will automatically generate an error message. For more information, please see https://de.mathworks.com/help/matlab/ref/arguments.html.
Best wishes,
Harald
  2 Commenti
Max Heimann
Max Heimann il 14 Ott 2024
Hello, thanks for the reply. Unfortunately i need this to be gone for everyone in the team. The data is coming from an interface which is not going to change any time soon. So for the time being the input has to be a cellstring. Which would make this function the perfect solution. I am just wondering why a function cannot be used without throwing a warning. Is it considered deprecated or is it missused so often, that a default warning is necessary?
I like the arguments block, but i would still want to validate the input data to be of the correct format. So im not sure if there is a suitable validator function to replace iscellstr?
Harald
Harald il 14 Ott 2024
If it is a reasonably small team, everybody could suppress the warning.
If the data is coming in as cell array of chars and you don't mind to continue working with strings afterwards, you could use arguments blocks like this:
function strOut = strfun(strIn)
arguments
strIn string
end
strOut = strIn;
Then, both
strfun({'abc', 'def'})
strfun(["abc", "def"])
would give
1×2 string array
"abc" "def"
Best wishes,
Harald

Accedi per commentare.

Categorie

Scopri di più su Characters and Strings in Help Center e File Exchange

Prodotti


Release

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by