Quantcast
Channel: MATLAB Central Newsreader Recent Posts
Viewing all articles
Browse latest Browse all 19628

Updating LEGEND in loop after CLA

$
0
0
I've got a custom function similar to COMET (shows an animation of a trajectory). After a few iterations, I clear the figure using CLA and start plotting again. This is all happening in a for loop.

Being plotted in the figure is the trajectory and the target (where the trajectory should end up). I just ran into a situation where the target doesn't exist for a sample and I want the legend to reflect this. I was hoping to remove the "Target" entry from the legend and then put it back in on subsequent trials when it does exist. That seemed impossible. Alternatively, I was hoping to use SET to change the text of the entries, changing "Current Target" to "NO TARGET"
However, when I do that, the strings are condensed in the legend and displayed one on top of the other. This doesn't happen if I don't use CLA. (I haven't looked into using delete instead of CLA to remove old targets, but I'm guessing it'd still be a problem because the handles to the object would go away.)

I've condensed the code I'm running into this snippit.

UseCLA = true; %toggle this to see the desired/undesired behavior in the legend

anim_fig = figure('name','Animation','toolbar','figure');
set(anim_fig,'renderer','painters');
set(anim_fig,'DoubleBuffer','on');
hold on
axis([-1 1 -1 1 -1 1])
axis vis3d
axis square

%% Set up and display the legend
empty = plot3(NaN,NaN,NaN,'LineStyle','none');
legend_colors = [empty];
legend_strings = {' LEGEND '};

traj_start = plot3(0,0,0,...
'Marker','o','LineStyle','none','MarkerEdgeColor','b');
traj_end = plot3(1,1,1,...
'Marker','x','LineStyle','none','MarkerEdgeColor',[0.1 0.8 0.1]);
legend_strings = {legend_strings{:}, 'Starting Position','Ending Position'};
legend_colors = [legend_colors; traj_start(1);traj_end(1)];

targeted_goal = plot3(0,0,0,...
'Marker','p','LineStyle','none','MarkerFaceColor','y',...
'MarkerEdgeColor','k','MarkerSize',10);
legend_strings = {legend_strings{:}, 'Current Target'};
legend_colors = [legend_colors; targeted_goal(1)];

hleg = legend(legend_colors,legend_strings{:},'Location','NorthEastOutside');
if(UseCLA)
cla(anim_fig);
end

animates_per_figure = 3; %how many to plot before we clear the screen and do it again
EmT = 0; %Remembers if last target existed
Targets_Exist = [0 1 1 0 0 1 1 1 0]; %A practice vector of whether targets exist or not
%% Done with setup. Now let's actually loop through all the trials.
for trial_index = 1:length(Targets_Exist)
if(~mod(animated_trials,animates_per_figure))% After we've plotted a certain amount of reaches, clear the screen and continue animating the data
if(UseCLA)
cla(anim_fig);
end
end

TargExist = Targets_Exist(trial_index);
if(TargExist ~= EmT)
%Change the legend
EmT = ~EmT;
if(EmT)
legend_strings{4} = 'NO TARGET';
else
legend_strings{4} = 'Current Target';
end
set(hleg,'String',legend_strings);
end

end %Close animates_per_figure loop


All suggestions, ideas, work arounds, kludges welcome.

Viewing all articles
Browse latest Browse all 19628

Trending Articles