add a number to the legend

Hi,
I want to add a legend to the plot so that each time the button in GUI is pressed the legend will show a string with corresponding number.
I want to work it like this:
First plot: Sandwich panel(1), second plot: Sandwich panel(2), etc...
My current code:
semilogx(f,TL,'DisplayName','Sandwich panel');
grid on
ylabel('Transmission Loss [dB]');
xlabel ('Frequency')
xlim([80 10000])
title('Transmission Loss')
legend('-DynamicLegend','location','best')
hold all
Thanks,
Dominika

Answers (2)

legend(['Sandwich panel(',num2str(put_number_here),')'])
or you could use sprintf.

2 Comments

Thanks for an answer Sara but I want Matlab to do it automatically, each press of the button in GUI will produce a string 'Sandwich panel' with a counting number.
Dominika, do you mean you need a way to count how many times the button has been pressed? In that case, you'll have to create a variable that is incremented any time you press the button, set it to zero in the opening_funct of the gui, and increment it in the pushbutton callback. Make sure to have:
guidata(hObject, handles);
at the end of the callback. Does this help?

Sign in to comment.

Continuing with Sara's comment:
In your callback function,
N_pushes = N_pushes + 1;
my_str = sprintf('Sandwich panel %g',N_pushes);
legend(my_str);

5 Comments

It seems to be exactly what I need but how can I get the handle of N_pushes from Opening function to the button Callback?
Thanks!
You set it in the opening function but not as a stand-alone variable, but rather as a member of handles. so it'll be something like: handles.N_pushes = 0; in the opening function And accordingly, you increment handles.N_pushes.
I put in the Opening Function:
handles.N_pushes = 0;
and in the Callback Function:
handles.N_pushes = handles.N_pushes + 1;
my_str = sprintf('Sandwich panel %g',handles.N_pushes);
semilogx(f,TL,'DisplayName','my_str');
grid on
ylabel('Transmission Loss [dB]');
xlabel ('Frequency')
xlim([80 10000])
title('Transmission Loss')
legend('-DynamicLegend','location','best')
hold all
guidata(hObject, handles);
The error: Reference to non-existent field 'N_pushes'.
Could you tell me what did I do wrong?
Thanks!
Do you have the instruction
guidata(hObject, handles);
at the end of EACH callback, including the opening function? Otherwise the variables you add to handles are not saved.
Works like a charm.
Thank you so much.
Have a nice day!

Sign in to comment.

Tags

Asked:

on 20 May 2014

Commented:

on 22 May 2014

Community Treasure Hunt

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

Start Hunting!