adding space before capital letters in variable names

25 ビュー (過去 30 日間)
Sara
Sara 2012 年 12 月 18 日
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
Jan 2012 年 12 月 18 日
Please use meaningful tags only. I do not see a connection to "legends" or "varargin" here.

サインインしてコメントする。

採用された回答

Image Analyst
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:

その他の回答 (4 件)

Daniel Shub
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)}')

Ryan G
Ryan G 2012 年 12 月 18 日
Try the code in this thread:
% 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
Walter Roberson 2012 年 12 月 18 日
I wrote that?? I must have been frustrated with someone and deliberately being obscure...
Image Analyst
Image Analyst 2012 年 12 月 18 日
Wow - clear as mud to me!

サインインしてコメントする。


Jan
Jan 2012 年 12 月 18 日
編集済み: Jan 2012 年 12 月 18 日
S = 'varNameOne';
up = isstrprop(s, 'upper');
T = blanks(length(S) + sum(up));
T(cumsum(up + 1)) = lower(S);

Jan
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
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.
Sara
Sara 2012 年 12 月 18 日
No, this isn't homework. It's part of a function which turns takes the name of input variables and uses that to create legend entries and filenames (no, NOT dynamic variable names). I wrote another question about five minutes before this one which gave more background detail, but accepted an answer too quickly. The input variable names do use varargin, and this IS for formatting legend entries automatically, but I see your point about tags.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLegend についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by