0
votes

I am trying to fetch a column from excel with rows more than 17500. Now problem is that when i call it in MATLAB , it does not gives me whole matrix with all data. it fetches data from somewhere in middle.

Now the real problem is that i have to add up 4 numbers in the column and get average , save it in another column and proceed to next consecutive set of numbers and repeat again till the end..How could i do that in MATLAB .Please help me solve this problem as i am just a rookie. Thank you.

so far i have done is this:

clc
g=xlsread('Data.xlsx',1,'E1:E17500');
x=1;
for i = 1:(17500/4) %as steps has to be stepped at 4 since we need avg of 4      
      y{i}=((g{x}+g{x+1}+g{x+2}+g{x+3})/4); 
      x=x+4;
end
xlswrite('Data.xlsx', y, 1, 'F1:F4375');
1
The for loop is incorrect. Instead of iterating i from 1 to 17497 at an interval of 4, you are iterating from 1 to 4275 at an interval of 1. Change it to 1:4:17500. - bcdan

1 Answers

0
votes

I see several things here: xlsread with one output gives you a numeric matrix of doubles (not a cell-array). Therefore you should address entries with () and not with {}. The for-loop can be omitted when we use reshape to create a matrix with dimensions 4x4375. The we calculate the average of the 4 values in each column directly with mean (evaluated over the first dimension). To get a column-vector again we have to transpose the result of mean using '.

Here is the code:

g = xlsread('Data.xlsx',1,'E1:E17500');
y = mean(reshape(g,4,[]),1)';
xlswrite('Data.xlsx',y,1,'F1:F4375');

To see in detail what happens within the code, let's see the results of each step using random data for g:

Code:

rng(4);
g = randi(10,12,1)
a = reshape(g,4,[])
b = mean(a,1)
y = b'

Result:

g =
    10
     6
    10
     8
     7
     3
    10
     1
     3
     5
     8
     2
a =
    10     7     3
     6     3     5
    10    10     8
     8     1     2
b =
    8.5000    5.2500    4.5000
y =
    8.5000
    5.2500
    4.5000