Storing Columns of Different Sizes in a Matrix
    8 visualizzazioni (ultimi 30 giorni)
  
       Mostra commenti meno recenti
    
    SJ B
 il 21 Mar 2016
  
    
    
    
    
    Commentato: Braden Kartchner
 il 10 Dic 2019
            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 Commenti
Risposta accettata
  Charles Dunn
      
 il 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 Commento
  Braden Kartchner
 il 10 Dic 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.
Più risposte (0)
Vedere anche
Categorie
				Scopri di più su Spreadsheets in Help Center e File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


