Convert variable to a title - includes special charcters

I am storing a file name as a variable to be retrieved. But, the name of the file contains underscores. When I use the variable name as the title of my plot each letter that follows an underscore becomes subscripted. What I would like is for the underscores to remain underscores. Example: FileName=SD_Padres_are_great;
Then I plot a chart & I use this as the title: title(FileName);
In the above example the output title would print with the underscores missing and the P a & g subscripted.
Does anyone know how to keep the title in the exact format of the FileName? BTW-I do not have control of the file names to change them as they are created by another program.

 採用された回答

Jacob Matthews
Jacob Matthews 2015 年 2 月 23 日

0 投票

You could use the command "strrep" to replace the underscores with spaces:
strrep(FileName,'_',' ')
or if you want to keep the filename exact with the underscores, insert a backslash:
strrep(FileName,'_','\_')
which will print the underscore as an underscore instead of treating it as a indicator for subscripting.

1 件のコメント

Guillaume
Guillaume 2015 年 2 月 23 日
Or you could just tell title to not interpret the string as Tex... (set the 'Interpreter' to 'none').

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

その他の回答 (1 件)

Guillaume
Guillaume 2015 年 2 月 23 日

1 投票

title(string_with_underscores, 'Interpreter', 'none')
will solve it. It's in the doc!

4 件のコメント

MathKat
MathKat 2015 年 2 月 23 日
This did not work for me. I'm not sure why. Thank you for trying.
Guillaume
Guillaume 2015 年 2 月 23 日
Then, you'll have to show the code that you've used. Most likely, you've made a mistake because, as mentioned that's the documented way to solve your issue.
figure;
plot(0:1, 0:1);
title('a_string_with_underscore', 'Interpreter', 'none');
works fine for me.
Note that there are other characters that can give you trouble if you leave the 'Interpreter' as default (the default being 'tex'). Again, read the doc, under the section Named-Value pair argument.
MathKat
MathKat 2015 年 2 月 23 日
編集済み: MathKat 2015 年 2 月 23 日
I'm sure it is user error :) BTW - could it be because I am running on 2012b?
Thx again.
Guillaume
Guillaume 2015 年 2 月 23 日
編集済み: Guillaume 2015 年 2 月 23 日
Nope, this has been the documented way since at least 2010.
Strike that, since at least 2003

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

カテゴリ

製品

質問済み:

2015 年 2 月 23 日

編集済み:

2015 年 2 月 23 日

Community Treasure Hunt

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

Start Hunting!

Translated by