0

what I need to do for this code is import a giant (302x11) excel doc, and then ask the user for an input. Then, I need to iterate through each element in the 5th column of the excel array, and if that element matches the user input, save the entire row to a new array. After going through all 302 rows, I need to display the new array.

So far, I have this:

Vin = input('Vin: ');
filename='MagneticCore.xlsx';
sheet=2;
xlRange='B2:L305';
[ndata, text, alldata] = xlsread(filename,sheet,xlRange,'basic');

After this, I'm not sure how to iterate through the alldata array.

Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324

2 Answers2

2

alldata is a cell, to select the fifth column you can use alldata{:,5}. Searching in Cells is done this way without iterating

Try it on your own, if you get stuck update your question with code and error message

Community
  • 1
  • 1
Daniel
  • 36,610
  • 3
  • 36
  • 69
0

Daniel R is right, you can do this without iterating through the cell array. Here's how you could iterate through the array if you needed to:

[ndata, text, alldata] = xlsread('Book1.xlsx');

target = 12;
newArray = {};
for r = 1:size(alldata, 1)
    % get the element in the fifth column of the current row
    e = raw{r,5};
    if e == target
        % add to newArray
        newArray{end + 1} = alldata(r,:);
    end
end

% display newArray
for r = 1:size(newArray, 1)
    disp(newArray{r})
end
Molly
  • 13,240
  • 4
  • 44
  • 45