How do I iterate through excel cells?

6 views (last 30 days)
Siddharth Baranwal
Siddharth Baranwal on 10 Sep 2019
Edited: Seth Furman on 1 Oct 2019
Hey Matlab Fam,
I'm trying to update an inventory file and read in an excel file but the xlsread function shows all strings as NaN values (unfortunately some item codes are strings). I need to be able to compare values from 2 columns in different excel files and they have strings. When I use the [num,txt,raw] method, the if statement with the == operator doesn't work and shows:
Undefined operator '==' for input arguments of type 'cell'.
If I change from parentheses to curvy brackets, {}, the == operator works but then the message shows:
Matrix dimensions must agree.
For the code below, I imported the inventory_file and file1 and created 2 arrays: inventory_file_sku and file1_sku. I need to import file1's quantities into an array with the corresponding row number as the items in inventory_file_sku. file1 has more products than inventory_file so I need to pick the right ones from file1.
new_qty = [];
for i = 1:989
for j = 1:7083
if file1_sku{j,1} == inventory_file_sku{i,1}
new_qty(i) = file1_qty{j};
end
end
end
Can someone help? Thank you in advance!

Answers (1)

Seth Furman
Seth Furman on 13 Sep 2019
Edited: Seth Furman on 1 Oct 2019
In general, the use of xlsread is not recommended. Please consider using readtable, readmatrix, or readcell instead.
Note that readtable allows you to supply import options to specify the format of the file you are trying to read.
In order to determine if two strings or character arrays are equal, you can use the strcmp function.
>> strcmp('dog','snake')
ans =
logical
0

Community Treasure Hunt

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

Start Hunting!