Search code examples
matlabmatlab-figure

How to give titles to the different colours of Matlab graphs


This part of code, will create three different graphs in one frame, and they will all three have different colours. But I don't know which colour corresponds to which i.

figure('Name','function','NumberTitle','on')
hold all

for i = 1:N 
    plot(input_firingRate(i,:),output_firingRate(i,:))
    ax = gca;
    ax.XAxisLocation = 'origin';
    ax.YAxisLocation = 'origin';       
end

Is there any way to have within the frame a title-like way to distinct between different results and colours? For example:

Test i = 1 : 'r'
Test i = 2 : 'g'
Test i = 3 : 'b'

Solution

  • You can write the label for each plot to a cell array, and then pass the cell array to a single call to legend after the loop:

    N = 3;
    figure('Name','function','NumberTitle','on')
    hold all
    titles = cell(N,1);
    for i = 1:N 
        plot(1:100,randn(1,100))
        titles{i} = ['line ', num2str(i)];
    end
    legend(titles);