Creating a structure and fields in nested loops

2 vues (au cours des 30 derniers jours)
Achuan Chen
Achuan Chen le 12 Déc 2021
Commenté : KSSV le 13 Déc 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:
structure=
apples: [distinct varieties for apples]
pears: [distinct varieties for pears] ...
Here's my attempt:
data=readtable('fruitvegprices.csv');
m=1;%m,n,p are counters
n=1;
p=1;
items={'apples','pears','carrots','cabbage'};
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
continue
end
matrix{find(strcmp(data.item{i},items)),p}=data.variety{ia(j)};
p=p+1;
end
lst(1).(data.item{i})=matrix{find(strcmp(data.item{i},items)),:};
end
end
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!
  4 commentaires
Stephen23
Stephen23 le 12 Déc 2021
Is there a particular reason you need to use a structure? The data are very neatly organized into columns, so using a table would most likely be much easier for you when processing that data.
Achuan Chen
Achuan Chen le 12 Déc 2021
No not at all, it was the first thing that came to mind and I couldn't figure out another method.

Connectez-vous pour commenter.

Réponse acceptée

KSSV
KSSV le 12 Déc 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
items={'apples','pears','carrots','cabbage'};
% 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.
  3 commentaires
Achuan Chen
Achuan Chen le 12 Déc 2021
Nevermind, I solved this by using the unique function. Thank you very much!
KSSV
KSSV le 13 Déc 2021
Yes you need to use unique. Perfect.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Structures dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by