adding space before capital letters in variable names
23 次查看(过去 30 天)
显示 更早的评论
if I'm converting a variable name to a string in a function, how can I put a space before every capital letter in the input variable? eg, if the input is:
varNameOne
how can I convert this to
var name one
for formatting legend entries?
1 个评论
Jan
2012-12-18
Please use meaningful tags only. I do not see a connection to "legends" or "varargin" here.
采纳的回答
Image Analyst
2012-12-18
编辑:Image Analyst
2012-12-18
Thinking that this may be homework, here's a hint:
s = 'varNameOne'
upperCaseIndexes = s >= 'A' & s <= 'Z'
Some related File Exchange submissions:
0 个评论
更多回答(4 个)
Daniel Shub
2012-12-18
编辑:Daniel Shub
2012-12-18
You can use REGEXPREP. To just add a space before every uppercase letter
regexprep('varNameOne', '([A-Z])', ' $1')
To add a space before every uppercase letter and convert that letter to lower case
regexprep('varNameOne', '([A-Z])', ' ${lower($1)}')
0 个评论
Ryan G
2012-12-18
% a string
s='varNameOne';
% the engine
ix=ismember(s,'A':'Z');
Then input the space (credit to Walter from a newsgroup thread circa 2008)
V = s;
P = find(ix==1)-1;
N = ' ';
for i = 1:numel(P)
V = [diag(kron(V.',ones(1,P(i))));N;flipud(diag(kron(flipud(V.'),ones(1,length(V)-P(i)))))].'
P = P+1;
end
2 个评论
Walter Roberson
2012-12-18
I wrote that?? I must have been frustrated with someone and deliberately being obscure...
Jan
2012-12-18
Actually I assume that this is a homework, too. It will not be easy to submit one of these solutions without cheating. And of course your teacher knows this forum also. Nevertheless, it is a funny problem and I think, if you spend the time to find out, how each of the solution works, you will have learned enough for today. But do not expect, that we solve homeworks in general.
S = 'varNameOne';
T(1, 2:end) = lower(S);
T(2, isstrprop(S, 'upper')) = ' ';
T = reshape(T(~~T)), 1, []);
2 个评论
Daniel Shub
2012-12-18
I interpreted the for "formatting legend entries" part of the question as meaning it was not homework. Most doit4me questions aren't creative enough to come up with a reason to want what they are asking for.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Legend 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!