How to use string for a graph legend
9 次查看(过去 30 天)
显示 更早的评论
Hi All
I have a script that scans a directory for text files (they have unique names), imports all of them and then I plot each text file to it's own graph and then all of them to one graph.
I want to add a legend to the graph, and the name for each dataset is a portion of what the file name was.
I then end up with a variable, lets call it string,
string = '2016-07-12 12-28-45.txt','2016-07-12 13-22-44.txt','2016-07-12 13-54-10.txt','2016-07-12 14-27-33.txt','2016-07-12 14-59-05.txt'
How do I use that variable as a legend? If I use legend(string); it just calls one of the plots the entire string name, instead of seeing it as a comma separated list.
If I copy the data in the string and paste it into the "legend(argument)" it works just fine, I'm sure it's a data-type error. Can someone please help?
I have attached my script and two sample data files. The number of data files are not known. As you will be able to see from the code I am a Matlab newbie.
0 个评论
采纳的回答
dpb
2016-7-13
The expression as you've written it for string generates a list, not an array which is what you need to match up with the lines for legend. Use
str = {'2016-07-12 12-28-45.txt','2016-07-12 13-22-44.txt','2016-07-12 13-54-10.txt','2016-07-12 14-27-33.txt','2016-07-12 14-59-05.txt'};
instead; a cellstr array. Note that I didn't use string as a variable; it's an (undocumented) builtin Matlab function; not sure what effect it has aliasing it but I try to avoid doing so on general principles.
You've got an array of file names, should be able to use it rather than hardcoding them like above. As a matter of style, I'd eliminate the .txt as superfluous and excessive length from the labels...
更多回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Cell Arrays 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!