Info

This question is closed. Reopen it to edit or answer.

Add new a column with different number of rows in a matrix using for loop

1 view (last 30 days)
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

Answers (1)

Srivardhan Gadila
Srivardhan Gadila on 30 Sep 2020
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

Community Treasure Hunt

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

Start Hunting!