Effacer les filtres
Effacer les filtres

Function for sanitizing field name strings

2 vues (au cours des 30 derniers jours)
Martin Ryba
Martin Ryba le 25 Août 2023
Commenté : Rik le 25 Août 2023
We have a file parser function (Midas BLUE) that generates structure arrays for some of the keyword/value pairs that can occur. The existing code uses deblank() to get rid of trailing strings, but the code bombs when the keyword contains a period(.) for instance. Is there a function that is similar to what readtable() uses to parse out field names to always return a valid name?

Réponse acceptée

Rik
Rik le 25 Août 2023
s = 'name with spaces and a . in the middle';
matlab.lang.makeValidName(s)
ans = 'nameWithSpacesAndA_InTheMiddle'
  2 commentaires
Martin Ryba
Martin Ryba le 25 Août 2023
That's exactly what I need, the help also mentions the makeUniqueStrings function, which is good to use instead of the hack that's in the code right now. I just wish that function was easier to find; I tried using "sanitize" as a keyword and that didn't help me find it.
Rik
Rik le 25 Août 2023
I believe a function like this has existed in the Matlab code base since the introduction of jsondecode, although I'm not entirely positive on that point.
I don't know if I would have expected this function to show up with that search term, but I understand your point. Discoverability of functions is sometimes the hardest part of a programming language.
At least I could help this time, and perhaps now it will show up in google results with similar search terms.

Connectez-vous pour commenter.

Plus de réponses (0)

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by