[英]fetching data from excel in matlab
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. 我试图从excel中获取行数超过17500的列。现在的问题是,当我在MATLAB中调用它时,它不能为我提供所有数据的完整矩阵。 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. 现在真正的问题是我必须在该列中添加4个数字并取平均值,将其保存在另一列中,然后继续处理下一个连续的数字集,然后再次重复直到结束。.我该如何在MATLAB中进行操作。帮助我解决这个问题,因为我只是个菜鸟。 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');
I see several things here: xlsread
with one output gives you a numeric matrix of doubles (not a cell-array). 我在这里看到了几件事:具有一个输出的xlsread
给您一个双精度的数字矩阵(而不是一个单元格数组)。 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
. 当我们使用for循环可以省略reshape
创建具有尺寸的矩阵4x4375
。 The we calculate the average of the 4 values in each column directly with mean
(evaluated over the first dimension). 我们直接用mean
(在第一个维度上评估)计算每列中4个值的mean
。 To get a column-vector again we have to transpose the result of mean
using '
. 为了再次获得列向量,我们必须使用'
转置mean
的结果。
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
: 要详细了解代码中发生的情况,让我们使用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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.