iscellstr warning "To support string in addition to cellstr, include a call to 'isstring'" unavoidable?
5 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Max Heimann
il 11 Ott 2024
Risposto: Steven Lord
il 14 Ott 2024
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
il 11 Ott 2024
Hi @Max Heimann
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?
Risposta accettata
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.
0 Commenti
Più risposte (1)
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
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
"abc" "def"
Best wishes,
Harald
Vedere anche
Categorie
Scopri di più su Characters and Strings in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!