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!