 Two questions about plotting - legends and tick marks values

Using the following code:

subplot (2,1,1)
hold on
plot (msecs, target, 'g');
[ax, h1, h2] = plotyy(msecs, pos, msecs, posPID);
set (ax(2), 'YLim', [-2500 2500]);
grid on;
xlabel('time (sec)');
ylabel(ax(1), 'Position');
ylabel(ax(2), 'PID');
titletext = ['Position and PID versus time ', dattim];
title(titletext);
legStr = {'Position', 'Pos PID'};
legend (legStr);
hold off

produces output like this:

The legend isn’t quite what I want, how can I get it to display a legend withTarget as Green, Position as Blue, PID as Orange

The other question is how to change the LH Axis tick annotations to use e.g. 4,610,000 instead of 4.61e+06?

Thanks
David

For the first question, see e.g. demo legend 27.

For the second question, try setting "yticklabel" to the strings you’d like to be displayed. (Those will be static though. So do that as the last step when finishing your plot.)

You should just provide “Target” as the first legend label.

Use the yticklabels to set label manually.
Here is an example:

plot (rand (1,3) * 4e4 + 4e6, "-g")
h_left_axes = gca ();
hold on
plotyy (1:3, rand (1,3) * 4e4 + 4e6, 1:3, rand (1,3))
legend ({"Target", "Position", "Pos PID"})
yt = yticks (h_left_axes);
ytl = yticklabels (h_left_axes);
for ii = 1:numel (yt)
ytl{ii} = sprintf ("%d", yt(ii));
## insert comma as thousand separator if needed
endfor
yticklabels (h_left_axes, ytl)

What’s the magic trick to do that? Is there a special printf mask that will insert thousands separators?

Not that I know of. The posix “%'d” format specifier (see e.g. here) is not implemented. The following function should do what you want:

function str = comma_thousand_sep (num)
str = double (sprintf ("%d", num));
ndigit  = ceil (numel (str) / 3) * 3;
warning ("off", "Octave:num-to-str", "local");
tmp = prepad (str, ndigit, "X");
tmp = [repelem(",", 1, ndigit/3); reshape(tmp, 3, ndigit/3)](2:end);
str = tmp(tmp != "X");
endfunction