str = {strcat('z = ' , num2str(z))} % at the end of first loop, z being loop output str = [str , strcat('z = ' , num2str(z))] % after 2nd loop % plot your data legend(str{:})