hi,guys,
my code is to plot 3 sets of data,in blue ,red and green respectively, however, the legend is not match for the third one (it is the same with the 2rd one), can anyone help me?
--------------------------------------------------------------------------------------------------
clear
filename = '1.log';
set = 768;
fid = fopen(filename,'r');
[num1,num2] = textread(filename,'%d%d');
lines = get_lines(fid);
length = 1:1:lines;
title('haha');
xlabel('hehe');
ylabel('heihei');
plot(length,num1,'-b*',length,set,'-r',length,num2,'-go');
hleg = legalpha('1','2','3');
fclose('all');
the results:

 채택된 답변

Michael Haderlein
Michael Haderlein 2014년 9월 16일

0 개 추천

Huh, there's a lot to comment.
1. To your question: Let me copy the important lines:
set = 768;
length = 1:1:lines;
plot(length,num1,'-b*',length,set,'-r',length,num2,'-go');
"set" has only one value, length has multiple values. When you do something like
plot(1:3,1,'-r'), legend show
you'll create 3 plots with equal style. The only thing which puzzles me is that you can see the red line. I expected no red line to be visible as each plot has only one point and there's no line to print.
What you can do is to plot it like this:
plot([1 lines],[set set],'-r')
Then, please see some additional comments:
2. "length" is a built-in function. Don't use this as variable name as this might cause odd and unexpected behavior of your program.
3. If you create an array with an increment of 1, you don't need to specify this increment. Just myarray=1:lines is fine (I don't want to use "length" as variable name again).
4. I don't know this get_lines function, but as you have already read the content of the file, a simple
lines=length(num1);
will do the same job. As long as you didn't create a variable called length ;-)
5. Close the file soon as possible (here, right after the textread) and just specify the file identifier.
fclose(fid);
If you work with multiple files, a general fclose('all') might cause errors which are tricky to find.
Some of these remarks might sound pedantic but learning a good programming style from the beginning will help you a lot later.

댓글 수: 3

Justin
Justin 2014년 9월 16일
thanks Michael! I modified my code as:
------------------------------------------------------
clear
filename = '1.log';
set = 768;
fid = fopen(filename,'r');
[num1,num2] = textread(filename,'%d%d');
lines = length(num1);
myarray = 1:lines;
fclose(fid);
title('haha');
xlabel('hehe');
ylabel('heihei');
plot(myarray,num1,'-b*',[1 lines],[set set],'-r',myarray,num2,'-go')
hleg = legalpha('1','2','3');
------------------------------------------------
and it shows!
is this version looks better? lol
and one more question, why the title, xlabel, ylabel are not shown? did I put them in the wrong place?
Michael Haderlein
Michael Haderlein 2014년 9월 16일
It does :)
Yes, title, xlabel and ylabel are in the wrong place. plot() will delete them. First plot, then title and label the axes. Alternatively, first set "hold all" and then plot. That will also prevent the title and the labels to be deleted.
Justin
Justin 2014년 9월 16일
yes, solved! this is my first time to write matlab script, thank you so much!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Adam
Adam 2014년 9월 16일
편집: Adam 2014년 9월 16일

0 개 추천

length,set,'-r'
in your plot instruction will not give what you want.
you should use
[1 lines], [set set], '-r'
instead because your x and y must have the same number of values to be a single line plot rather than hundreds of individual plots.
Incidentally, don't call a variable 'length'. There is a function called length and even if it doesn't cause you problems here it is sure to in the future if you hide the function name under a variable name.

댓글 수: 1

Justin
Justin 2014년 9월 16일
thank you! you catch the point! but Michael's answer corrects me more, so please forgive me to give the 'accept answer' to him. thanks again!

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

질문:

2014년 9월 16일

댓글:

2014년 9월 16일

Community Treasure Hunt

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

Start Hunting!

Translated by