Storing Columns of Different Sizes in a Matrix
5 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Charles Dunn
2016-3-22
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 个评论
Braden Kartchner
2019-12-10
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!