user2933750
user2933750

Reputation: 1

How do I iterate through an imported excel doc?

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.

Upvotes: 0

Views: 228

Answers (2)

Molly
Molly

Reputation: 13610

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

Upvotes: 0

Daniel
Daniel

Reputation: 36720

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

Upvotes: 2

Related Questions