Storing Columns of Different Sizes in a Matrix

4 views (last 30 days)
I am importing data from an excel workbook with various sheets. I am trying to look at the product of particular columns and put them in a new matrix. However, the lengths of the columns differs from sheet to sheet and I seem to only be able to get the product for the last column (I'm assuming this is because of the size discrepancy but I might be indexing wrong). I'd appreciate any help. I'm not sure what I'm missing. I can provide more code ideas but I'm stuck.
%Get in correct directory with Excel File
[status,sheets]=xlsfinfo('blahblah.xls');
a=numel(sheets);
for d=1:a
[b,c]=xlsread('blahblah.xls',d); %Read excel file
x=b(:,1); %Choose columns for outputs
y=b(:,2);
P=ones(size(y,1),a);
P=x.*y; %P=[P x.*y] maybe but different column sizes in sheets
end

Accepted Answer

Charles Dunn
Charles Dunn on 22 Mar 2016
You could use cell arrays. Cell arrays allow you to store data of different types (strings, ints, doubles, arrays, etc.) all in the same data structure. They also allow different sized arrays to be stored together.
%Get in correct directory with Excel File
[status,sheets]=xlsfinfo('blahblah.xls');
a=numel(sheets);
%be sure to initiate the cell array, just like you would for an array
P = cell(1,a);
for d=1:a
[b,c]=xlsread('blahblah.xls',d); %Read excel file
x=b(:,1); %Choose columns for outputs
y=b(:,2);
%store the result in P
P{d}=x.*y;
end
  1 Comment
Braden Kartchner
Braden Kartchner on 10 Dec 2019
If you are using v.2019a or later, you would want to use "readmatrix" or "readcell" instead of xlsread.
Unless you are wanting backwards compatability, in which case, xlsread is fine.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!