How do i prevent previous legend entries to get erased

10 views (last 30 days)
i have a code which is used to analyse some raw data data and compare the results of many(unknown number) of tests. for this my code first starts like this
prompt='how many files do you wish to analyse? ';
prompt1='what is the name of the test? ';
prompt2='what is the name of the file? ';
n=input(prompt);
for i=1:n
name(i,:)=input(prompt1,'s');
filename(i,:)=input(prompt2);
end
for i=1:n
a(:,:,i)=ftp_75_2(filename(i,:));
for j=1:18
figure(j)
legend(name(i,:));
end
end
the trouble is every time a new legend gets added the old one gets removed. can someone help me. I have seen some solutions online but they all require the person who runs the code to create a specific label command or to know the number of files which will be in the legend rather than take an input from the user.
I am creating this code to give prompts since the people who are to use this will have little to no matlab knowledge

Answers (1)

Joseph Cheng
Joseph Cheng on 10 Jun 2015
Off the top of my head it is not part of the legend function. However it doesn't stop us from modifying what is already there.
for ind = 1:3
x = randi(10,10,5);
figure(ind),plot(x);
hleg(ind) = legend('toggle');
end
names = {'line 1','line 2','line 3','line 4','line 5'};
for ind = 1:size(x,2)
for jnd = 1:3
legStr = get(hleg(jnd),'string');
legStr(ind) = names(ind);
set(hleg(jnd),'string',legStr);
end
end
  2 Comments
SamwiseGamgee
SamwiseGamgee on 10 Jun 2015
The number of files which shall be processed through my code is a user input and hence the number of loops and legend entries cannot be fixed.
Joseph Cheng
Joseph Cheng on 11 Jun 2015
and? if you played around with the code you can see how to add items using set or legend. I have no idea what your ftp function does so i assumed it added plots to the line but the legend markings were done after the plots were generated. So.... here is a modification of the things i did before.
clc
clear all
close all;
hfig = figure(1);hold on;
n=1;
while 1
answer = questdlg('add another plot?', ...
'add plot', ...
'yes', 'no','yes');
switch answer
case 'yes'
hleg = legend;
plot(randi(10,1,10))
hlegstr = get(hleg,'string');
hlegstr{n} = ['plotnum ' num2str(n)];
legend(hlegstr)
n=n+1;
case 'no'
break
end
end

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!