create a struct based on user id

조회 수: 3 (최근 30일)
elisa ewin
elisa ewin 2016년 5월 30일
댓글: Andrei Bobrov 2016년 5월 30일
Hi! I have a set of location records called LocationRecords = [usersID, years month, day, hour, minuts, seconds, latitude, longitude, regionID], attached. I want to create a struct of data in which I can put the date associated to every user id. E.g If I have
LocationRecords=[14 2009 3 17 16 14 2 0.528229037988558 -1.70585947774536 9247;14 2009 3 17 2 33 10 0.528288412470045 -1.70598280759199 9465;14 2009 3 17 2 32 24 0.528288412470045 -1.70598280759199 9313;62 2009 3 15 19 17 34 0.528256895934005 -1.70598404814585 9223;89 2009 3 15 17 41 20 0.528261141300802 -1.70596368432656 9250]
I will obtain a struct 1x3 in which put the data associated to user with id=14, id=62 and id=89, can you help me?
  댓글 수: 2
Stephen23
Stephen23 2016년 5월 30일
편집: Stephen23 2016년 5월 30일
@elisa ewin: how do you want repeated IDs handled? Most of the IDs occur on multiple lines.
elisa ewin
elisa ewin 2016년 5월 30일
I want to repeated IDs in every row; example
struct<1,1>=[14 2009 3 17 16 14 2 0.528229037988558 -1.70585947774536 9247;14 2009 3 17 2 33 10 0.528288412470045 -1.70598280759199 9465;14 2009 3 17 2 32 24 0.528288412470045 -1.70598280759199 9313]
struct<1,2>=[62 2009 3 15 19 17 34 0.528256895934005 -1.70598404814585 9223]
struct<1,3>=[89 2009 3 15 17 41 20 0.528261141300802 -1.70596368432656 9250]

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

채택된 답변

Andrei Bobrov
Andrei Bobrov 2016년 5월 30일
편집: Andrei Bobrov 2016년 5월 30일
[a,~,b] = unique(LocationRecords(:,1));
x = cellfun(@(x)sprintf('id_%d',x),num2cell(a),'un',0);
y = accumarray(b,(1:numel(b))',[],@(x){LocationRecords(x,2:end)});
C = cell2struct(y,x,1);
  댓글 수: 2
elisa ewin
elisa ewin 2016년 5월 30일
it run, but give an error in code row
C = cell2struct(y,x,1);
Error using cell2struct
Invalid field name "14"
Andrei Bobrov
Andrei Bobrov 2016년 5월 30일
Corrected

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by