Creating a structure and fields in nested loops

5 views (last 30 days)
Achuan Chen
Achuan Chen on 12 Dec 2021
Commented: KSSV on 13 Dec 2021
I'm extracting information from a large Exel sheet, it has headers 'category, 'item', 'variety', 'date', 'price', 'unit'; the 'item' header contains fruits and vegetables, and 'variety' contains the different varieties, and 'item' and 'variety' columns are the only 2 concerned in this task.
I'm trying to create a structure with field names 'apples', 'pears', 'carrots, 'cabbage' and for each field it contains an array of distinct varieties of the corresponding item, so it looks something like:
apples: [distinct varieties for apples]
pears: [distinct varieties for pears] ...
Here's my attempt:
m=1;%m,n,p are counters
matrix=cell(length(items),1);%creates 4-row matrix, where each row
%contains distinct varies of each corresponding item
[A, ia, ic]=unique(data.variety,'stable');
for i=1:length(data.item)
if ismember(data.item{i},items)%checks if it's the item we want
for j=1:length(ia)
if strcmp(data.item{ia(j)},data.item{i})==0 %checks if variety correponds to the correct item
My approch was to first obtain a list 'A' of distinct varies for all items, then loop through 'ia' to identity any duplicated varieties for each of the 4 items. If succesful I would obtain a 4-row matrix whose rows are the distinct varieties for the 4 items, then I could assign the corresponding row and structure field.
However, this did not quite work out the way I planned, I think it's giving me a matrix of ALL varities, and the output for the structure is not as anticipated too:
Could someone enlighten me?
Thank you very much!
Achuan Chen
Achuan Chen on 12 Dec 2021
No not at all, it was the first thing that came to mind and I couldn't figure out another method.

Sign in to comment.

Accepted Answer

KSSV on 12 Dec 2021
You can use ismember. I don't think you need to use a loop here.
T=readtable('fruitvegprices.csv'); % first column of T is items and second is variety
% Get apples
[idx,ia] = ismember(T.(1),items{1});
T_apples = T(idx,:) ; % this gives table of apples alone
Now you can convert it to structure using table2struct.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by