Using strings in regular code.
1 次查看(过去 30 天)
显示 更早的评论
Simple question. I've looked around the documentation and can't seem to find this.
How do I use a string and pass it into code?? Example:
%I create a string
Str = Superman
then how do I pass this string in the saveas command? So instead of writing this line of code:
saveas(gcf,'Superman.fig');
I would write something to the effect of:
saveas(gcf,''Str'.fig') or something?!?!??!?!?
Thanks!
0 个评论
采纳的回答
F.
2012-7-5
For me, to construct the name of the file (it's what I use)
FileName = 'Superman' ;
FileDir = 'I:\..\MyDir'
FilePath = fullfile( FileDir, sprintf( '%s.fig', FileName ));
saveas(gcf,FilePath)
You can write this in one line if you want
6 个评论
Thomas
2012-7-5
You could also try
Str = 'Superman'
filename=sprintf('%s.fig',Str)
saveas(gcf,filename)
Kevin Claytor
2012-7-5
Strings are indicated by single quotes; ''
You can concatenate strings with either the strcat(s1,s2,...) command or the straight brakets; [].
So the code would look like;
str1 = 'superman'
str2 = '.fig'
comb1 = strcat(str1,str2)
comb2 = [str1, str2]
The output of both gives you "superman.fig". To use in a call to another function you can just use one of the combined strings;
saveas(gcf,comb1)
Or if you wanted to save some clutter, as those above did;
saveas(gcf,[str1,'.fig'])
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!