sprintf of a variable and string name

it's needed to display a variable string name. for example:
for i=1:w I have these:
i=1 >>>> name='t56757w_e_l_l'
i=2 >>>> name='h_a_h_a_h_a067'
i=3 >>>> name='a56754no_the_r'
.
.
.
't56757w_e_l_l' and 'h_a_h_a_h_a067' and 'a56754no_the_r' ,... are the names of matrix.
Problem: its needed to plot a figure in these names that i syaed above but when i use:
figure('Name',sprintf('%s',name))
sprintf('%s',name) gives numbers of arrays of matrix and not the name of that.

댓글 수: 2

Fangjun Jiang
Fangjun Jiang 2011년 9월 17일
You might have many variables. How do you know 't56757w_e_l_l' is corresponding to i=1, 'h_a_h_a_h_a067' is corresponding to i=2, etc.?
mohammad
mohammad 2011년 9월 17일
because I give these names manually in each loop.
in other words in each loop, it asks to import a .xls file and read this file and that must put in a matrix that name of this matrix must be same with .xls file name.
so these name in each loop are the names of .xls files

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

 채택된 답변

Fangjun Jiang
Fangjun Jiang 2011년 9월 17일

1 개 추천

In that case, the name is actually given manually. To avoid entering the same name twice, maybe you can do this.
Name='t56757w_e_l_l';
Data=xlsread([Name,'.xls']);
assignin('base',Name,Data);
sprintf('%s',Name)
You know the name of the variable. You need to use this variable. You also want to use the variable name in its string format.
The name sounds very irregular. You have to specify it manually. Why can't you also specify the string since you have already specified the variable name? It doesn't make a difference if you specify it once, or twice. Like.
plot(t56757w_e_l_l); title('t56757w_e_l_l');
Or, if you are willing to make a function, you can do this
function PlotAndTitle(a) plot(a); title(inputname(1));
You can call it this way:
PlotAndTitle(t56757w_e_l_l)

댓글 수: 6

mohammad
mohammad 2011년 9월 17일
I did exactly this in another function m-file. now 't56757w_e_l_l' is exist in workspace. but in this function( plotting(Name)) that i want plot 't56757w_e_l_l', 'Name' converts to data!!! and this is really strange. there is no problem in this command:plot(Name)
because 'Name' is data (i mean a matrix)!
but it must be a string. problem there is where i want to determine name of figure that i want name of figure to be 't56757w_e_l_l'
Fangjun Jiang
Fangjun Jiang 2011년 9월 17일
see update
mohammad
mohammad 2011년 9월 17일
very nice
i need to make function because there are more than 300 .xls files the names of these must automatically be the names of matrix too, in a for-loop.
with your helping its solved:
name=[inputname(1),'and' inputname(2)]
set(gcf,'Name',name)
Image Analyst
Image Analyst 2011년 9월 17일
I presume by now you've noticed that underlines in the string convert the following characters to subscripts in the title() function. Doesn't happen for figure() though - the underlines show up in that case.
Fangjun Jiang
Fangjun Jiang 2011년 9월 17일
Good catch, Image Analyst! I didn't pay attention to the underscore effect in the title() string.
Image Analyst
Image Analyst 2011년 11월 1일
If you want to have an actual underline in the string, you can replace the underline with a backslash underline with this code:
yourString = strrep(yourString , '_', '\_');
before you call title() or text().
This will make sure you get the underline to show up when you use title() and text().

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

추가 답변 (1개)

Malcolm Lidierth
Malcolm Lidierth 2011년 9월 17일

0 개 추천

No. sprintf(%s,name) gives:
Error: Expression or statement is incorrect--possibly unbalanced (, {, or [.
(In fact missing single quotes). Try pasting the actual code.
figure('Name',sprintf('%s',name)) works, and will make sense if name is a char array.

댓글 수: 1

mohammad
mohammad 2011년 9월 17일
Thanks dear, I used '%s', here i wrote wrong.

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

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by