필터 지우기
필터 지우기

Storing Columns of Different Sizes in a Matrix

조회 수: 9 (최근 30일)
SJ B
SJ B 2016년 3월 21일
댓글: Braden Kartchner 2019년 12월 10일
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

채택된 답변

Charles Dunn
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
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 CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by