How to use string for a graph legend

9 views (last 30 days)
AJBotha
AJBotha on 13 Jul 2016
Answered: AJBotha on 14 Jul 2016
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.

Accepted Answer

dpb
dpb on 13 Jul 2016
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 Comment
AJBotha
AJBotha on 14 Jul 2016
Hi dpb.
Thank you for helping, I still can't get it working. I am storing the file names in a long string called globalLabel - this is the whos' result for globalLabel:
whos globalLabel
Name Size Bytes Class Attributes
globalLabel 1x1 630 cell
In this variable I am using the current filename(well, a part thereof) and then I insert a single quote in front and behind the name, followed by a comma, and I do this for all instances except if it is the final entry then I just don't add the comma.
If I go to the Workspace window and open my variable (globalLabel) and right click copy-paste then this is the result:
'''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'',''2016-07-12 15-32-46.txt'',''2016-07-13 07-38-05.txt'',''2016-07-13 08-20-51.txt'',''2016-07-13 08-54-10.txt'',''2016-07-13 09-36-04.txt'''
If I display the variable from the Command Window I get the following result:
>> globalLabel
globalLabel =
''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','2016-…'
This is not working to Display the Legend:
if index == numDatasets
str = cellstr(globalLabel);
display(str);
legend(str);
end
Can you please help with the data type conversion?

Sign in to comment.

More Answers (1)

AJBotha
AJBotha on 14 Jul 2016
Instead of concatenating strings using the cellstr(attribute) function finally worked for me. Index increments by one for every file, fLogName is the name of the file and legendLabels contains an array of all the labels that I want on my graph.
legendLabels(index,:) = fLogName(:);
if numDatasets == index
str = cellstr(legendLabels);
display(str);
legend(str);
end
Thank you for the insight.

Categories

Find more on Cell Arrays in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!