필터 지우기
필터 지우기

How to print string or variable in plot title?

조회 수: 97 (최근 30일)
Daniel
Daniel 2012년 2월 3일
댓글: Image Analyst 2014년 4월 10일
I want to prompt for a file name, e.g. "4.dat", "6.dat", etc. I want to store it as a string to be used both to open that file -- e.g. importdata(fileToRead1) -- and to print later on in the title of a plot, e.g. title(y(x) vs x using fileToRead1), where fileToRead1 is instead '4.dat', '6.dat', etc. It would be even better if I could say "4 [some text]" instead of "4.dat", but beggars can't be choosers. It is possible, isn't it? But I can't figure it out:
filename = input('filename? ','s');
title('testing' {filename})
title('testing {filename}')
title('testing filename')
title(filename)
The first title command doesn't work, the middle two print 'filename' without braces; the last prints the string stored in filename. I think you know what I want to do; please help!

채택된 답변

Walter Roberson
Walter Roberson 2012년 2월 3일
filename = fileToRead1;
dotpos = find(filename == '.', 1, 'last');
if ~isempty(dotpos); filename(dotpos:end) = []; end
Then either
title(sprintf('y(x) vs x using %s', filename))
or
title(['y(x) vs x using ', filename]);

추가 답변 (1개)

Erik
Erik 2014년 4월 10일
i tried it in my m file. but it keeps saying that it does not understand filetoread1 and it gives a warning: '' the argument for the %s format specifier must be of type char ( a string) .
could it be the case that it is not applicable on the matlab version 2009?
  댓글 수: 1
Image Analyst
Image Analyst 2014년 4월 10일
Erik, when you take sample code and use it inside your own code, you have to change the variable names in the sample code to the actual variable names that you are using in the code you're inserting the sample code into.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by