Info
此问题已关闭。 请重新打开它进行编辑或回答。
Add new a column with different number of rows in a matrix using for loop
1 次查看(过去 30 天)
显示 更早的评论
Hi all,
How do I add a new column that has a different number of rows into a matrix using for loop.
I have 14 data sets (excel documents) where I want to take out the 7th column and add in a new matrix.
Right now MatLab tells me that it's unable to do so becasue the size of the left and right side are not the same.
Skript:
numfiles = 14; %number of files
col = 7; %column 7
for k = 1:numfiles
myfilename = sprintf('SK2_S%d_CTD.xlsx', k); %first file is named SK2_S1_CTD.xlsx, second SK2_S2_CTD.xlsx until 14th file SK2_S14_CTD.xlsx
data(:,k) = xlsread(myfilename,1,'G4:G48'); %data would be the new matrix containing cell G4:G48 (in excel) from each file
%data would have the size 48x14
end
0 个评论
回答(1 个)
Srivardhan Gadila
2020-9-30
As per my knowledge, I think the output of "xlsread(myfilename,1,'G4:G48')" would be 45x1 matrix and not 48x1. Refer to the documentation of xlsread.
You can initialize the data matrix based on the size of the data to be extracted as follows:
numfiles = 14; %number of files
rowSize = 45; %Change this value according to size of the extracted data
data = zeros(rowSize,numfiles);
for k = 1:numfiles
myfilename = sprintf('SK2_S%d_CTD.xlsx', k);
data(:,k) = xlsread(myfilename,1,'G4:G48');
end
0 个评论
此问题已关闭。
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!