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

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).

 Accepted Answer

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 Comments

Getting error message: "Cell contents assignment to a non-cell array object."...!
Which line gives that error?
Can you post a tiny example of a *.dat file?
I couldn't attach the .dat file so I am attaching the .txt version of the same file. Here it is. Thank you.
I saved that Data1.txt file into my directory, made a copy called Data2.txt, and the ran the following command with no trouble:
for nf = 1:2
B{nf} = load(['Data',num2str(nf),'.txt']);
end
Can you get that far? If so, then B should be a two-element cell array, where each cell is an Nx2 array:
B =
[8x2 double] [8x2 double]
After that, you just need to be careful to only refer to B{1}, B{2}, etc in your code. For example, you'll want to do
Dat=[B{nf}(:,1) B{nf}(:,2)];
inside of your for loop.
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?)
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)

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Asked:

on 1 Jun 2014

Commented:

on 3 Jun 2014

Community Treasure Hunt

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

Start Hunting!