Creating a structure and fields in nested loops

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

You forgot to attach 'fruitvegprices.csv'. I don't expect answers until you attach it.
Sorry I did forget, I've just uploaded it!
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.
No not at all, it was the first thing that came to mind and I couldn't figure out another method.

댓글을 달려면 로그인하십시오.

 채택된 답변

KSSV
KSSV 2021년 12월 12일

1 개 추천

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

Thanks. Slight problem, this creates a table of all occurences of apple, but I was trying to find the distinct varieties of each item
,
as shown in the output most of the entries in 'variety' are duplicates.
How can I pick out only the distinct varieties?
Thank you so much!
Nevermind, I solved this by using the unique function. Thank you very much!
KSSV
KSSV 2021년 12월 13일
Yes you need to use unique. Perfect.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

질문:

2021년 12월 12일

댓글:

2021년 12월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by