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!