フィルターのクリア

Use a character variable for a title in a plot function

24 ビュー (過去 30 日間)
balsip
balsip 2017 年 1 月 16 日
コメント済み: Image Analyst 2017 年 1 月 16 日
Hello, I would like to use a pre-defined variable within the title of a plot function. The code that doesn't work is:
Site = char('LabX');
Then within the function itself:
title([Site ', MeanRespValues']);
This returns the error:
Undefined function or variable 'Site'.
Error in plotFunction (line 28)
title([Site ', MeanRespValues']);
Ideally, in the function's prompt it could ask for a site name to be entered, rather than creating a variable.
I'm looking to return a plot with the title "LabX, MeanRespValues".
I appreciate any help you can throw my way.

採用された回答

Image Analyst
Image Analyst 2017 年 1 月 16 日
編集済み: Image Analyst 2017 年 1 月 16 日
First, make sure Site is in scope - that is, it's visible in the same function where you're going to call title(). It sounds like it's not. Use inputdlg() if you want to ask the user for it but again, do this in the same function where you call title().
Then, once Site is in scope, use sprintf() to create a character string that you can pass to plot():
caption = sprintf('%s, MeanRespValues', Site);
title(caption, 'FontSize', 25, 'FontWeight', 'bold', 'Color', 'b');
  2 件のコメント
balsip
balsip 2017 年 1 月 16 日
Image Analyst,
Thanks for your input. However, I was hoping to define Site without having to open up the function with title().
Is this possible?
Image Analyst
Image Analyst 2017 年 1 月 16 日
Yes, it's possible. If Site is defined in some other function, it will be local to that function and not seen by other functions, but there are ways to get it over to the function you need it in. See the FAQ for a variety of different ways. http://matlab.wikia.com/wiki/FAQ#How_can_I_share_data_between_callback_functions_in_my_GUI.28s.29.3F

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

その他の回答 (1 件)

John BG
John BG 2017 年 1 月 16 日
編集済み: John BG 2017 年 1 月 16 日
Balsip
assuming LabX is type double, uint, ..
use
Site=num2str(LabX)
no need for '' around the variable LabX
the reason is that the function char() doesn't cast the type you want, it produces the character that according to ASCII table has for numeric value 3.
if you find these lines useful would you please mark my answer as Accepted Answer?
To any other reader, if you find this answer of any help please click on the thumbs-up vote link,
thanks in advance for time and attention
John BG
  2 件のコメント
balsip
balsip 2017 年 1 月 16 日
Thanks, John. However, as with Image Analyst's answer below, I was hoping to define the Site variable outside of the function(), so that I don't have to edit the function script itself each time I want to change the plot's title as it is output from the plot function.
Any ideas here?
Image Analyst
Image Analyst 2017 年 1 月 16 日
Like I said, the FAQ shows you how to do it.

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by