iscellstr warning "To support string in addition to cellstr, include a call to 'isstring'" unavoidable?
4 次查看(过去 30 天)
显示 更早的评论
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 个评论
Hitesh
2024-10-11
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?
采纳的回答
Steven Lord
2024-10-14
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 个评论
更多回答(1 个)
Harald
2024-10-11
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 个评论
Harald
2024-10-14
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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!