필터 지우기
필터 지우기

How can I loop this code for several data files and finally accumulate all the plots together?

조회 수: 2 (최근 30일)
I have a code:
B=load('Data1.dat');
Dat=[B(:,1) B(:,2)];
nbins=[1000 1000];
n=hist3(Dat,nbins);
n1 = n;
n1(size(n,1) + 1, size(n,2) + 1) = 0;
xb1 = linspace(min(Dat(:,1)),max(Dat(:,1)),size(n,1)+1);
yb1 = linspace(min(Dat(:,2)),max(Dat(:,2)),size(n,1)+1);
figure
pcolor(xb1,yb1,n1);
xaxis= 0:0.1:40;
set(gca, 'Xticklabel', {xaxis})
set(gca, 'Yticklabel', {xaxis})
xx1=sum(n1);
I want to reapeat this code for Data1.dat, Data2.dat, Data3.dat,.......Datan.dat and finally put the results in a single plot like:
plot(xb1, xx1,':b',xb2,xx2,':r',......xbn,xxn,':k')
where xb2,xb3...xbn and xx2,xx3....xxn are the values of xb1 and xx1 in the code for second third etc... nth run(corresponding to each Data fle).

채택된 답변

the cyclist
the cyclist 2014년 6월 1일
There are several solid ways to do this. Here is a guide for some of the components of what you want to do.
Start your code with the commands
figure
hold on
which will set up one figure for all your lines.
Wrap your data loading and drawing commands (except for the figure command) in a loop
for nf = 1:7
% Your code
end
For loading different files inside the loop, do something like this:
B{nf} = load(['Data',num2str(nf),'.dat']);
[The sprintf command is also good to construct the file name.]
Refer to B{nf} rather than B later in the code.
The final figure will have lines that are all the same color (I think). Post another question or comment if you get this far and can't figure out how to fix that. Hint: This will help: http://www.mathworks.com/help/matlab/ref/set.html
  댓글 수: 6
aneps
aneps 2014년 6월 3일
편집: aneps 2014년 6월 3일
Thank you.. this works very nice....but when it plot all the plots appear in the same color so it is hard to identify different plots if I have nf=1:4 or 1:10...is it possible to include the code to give color to the plot for each 'nf' and with corresponding legend (for example, plot for nf=1 in blue, 2 in red, 3 in black and specify those in legends?)
the cyclist
the cyclist 2014년 6월 3일
There are many ways to handle this. Here is one very simple example:
figure
hold on
h = zeros(3,1);
for nf = 1:3
% Plot some random data
h(nf) = plot((1:5)+nf,rand(1,5),'.-');
% Set some random colors
set(h(nf),'Color',rand(1,3))
end
legend(h)

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by