Plot legend contours messed up

2 views (last 30 days)
dormant
dormant on 5 Mar 2025
Answered: dormant on 6 Mar 2025
I am creating a plot where the data to be plotted is calculated from data in a set of text files. The data is grouped for plotting based on a text string in the filenames. When I add a legend for these groupings ("clumps") the colours in teh legend are messed up.
The script is quite big and messy, so I've created a simpler test script and text files with just two clumps. These are in the attached zip file. The plot created by the test script is this:
Some of the text files don't have all the required data. The script will then try to plot [NaN NaN] for those files.
The legend is not messed up if I remove these text files. In the files provided, delete the first two "Scar" files and you get this:
Where am I going wrong?
UNfortunately, I cannot change the approach of constructing the plot from the text files. The text files are generated by other software and there can be several hundred of them, in up to 12 clumps.

Accepted Answer

dormant
dormant on 6 Mar 2025
After a bit of fiddling it was clear that my overall approach was totally wrong. My script had three tiled subplots and I was dancing around tiles and text files. Then I was trying to add the legend to each tile. Modifying this script was murder.
It's much easier to read all the text files into arrays or structures first. Then do the plots. I don't know why I didn't use this approach.
Thanks to all who posted. I'll come back with a new question if I run into problems with the new approach.

More Answers (1)

William Rose
William Rose on 5 Mar 2025
Edited: William Rose on 5 Mar 2025
Try the attached. Good luck.
Edit: Update the script, as descirbed in my comment below.
  6 Comments
Afiq Azaibi
Afiq Azaibi on 5 Mar 2025
Glad to hear my other answer was able to help, @William Rose. Thanks for bringing attention to it.
To echo the relevant information from that post, if you want certian objects to appear in a legend in a certain order, the best practice is to speicfy the objects in the legend() command. This syntax is referenced as subset in the doc and is what the William points out as my first method.
dormant
dormant on 6 Mar 2025
Thanks for that info.

Sign in to comment.

Tags

Products


Release

R2024b

Community Treasure Hunt

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

Start Hunting!