Question

I'll begin by saying I am really not good in programming especially in extracting data so please bear with me. I think my problem is simple, I just can't figure out how to do it.

My problem is I want to extract part of the data in a series of excel files stored in the same folder. To be specific, let's say I have 10 excel files with 1000 data in each (from A1:A1000). I want to extract the first 100 data (A1:A100) in each excel files and store it in a single variable with a 10x100 size (each row represents each file).

I would really appreciate if any of you can help me. This would make my data processing a lot faster.

EDIT: I have figured out the code but my next problem is to create another loop such that it will reread again the 10 files but this time extract A101:A200 until A901:A1000.

here's the code i've written:

for k=1:1:10
 file=['',int2str(k),'.xlsx'];
 data=(xlsread(file,'A1:A100'))';
 z(k,:)=data(1,:);
end

I'm not sure how i will edit this part data=(xlsread(file,'A1:A100'))' to do the loop i wanted to do.

Was it helpful?

Solution 2

This should do it:

for sec = 1:1:10
    for k=1:1:10
        file=['',int2str(k),'.xlsx'];
        section = ['A', num2str(1+(100*(sec-1)), ':A', mum2str(100*sec)]
        data=(xlsread(file, section))';
        z(k,:)=data(1,:);
    end
    output(sec) = z;
end

OTHER TIPS

my next problem is to create another loop such that it will reread again the 10 files but this time extract A101:A200 until A901:A1000.

Why? Why not extract A1:A1000 in one block and then reshape or otherwise split up the data?

data(k,:)=(xlsread(file,'A1:A1000'))';

Then the A1:A100 data is in data(k,1:100), and so on. If you do this:

data = data(reshape, [10 100 10]);

Then data(:,:,1) should be your A1:A100 values as in your original loop, and so on until data(:,:,10).

Here's a suggestion to loop through the different cells to read. Obviously, you can change how you arrange the collected data in z. I have done it as the first index representing the different cells to read (1 for 1:100, 2 for 101:200, etc...), the second index being the file number (as per your original code) and the third index the data (100 data points).

% pre-allocate data
z = zeros(10,10,100);

for kk=1:10
   cells_to_read = ['A' num2str(kk*100-99) ':A' num2str(kk*100)];
   for k=1:10
       file=['',int2str(k),'.xlsx'];
       data=(xlsread(file,cells_to_read))';
       z(kk,k,:)=data(1,:);
   end
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top