How to Search a struct array?

조회 수: 11 (최근 30일)
Dharmesh Joshi
Dharmesh Joshi 2022년 10월 25일
댓글: Dharmesh Joshi 2022년 10월 25일
Hi
I am new to Matlab and would like to know if i can search a array strucuture without using a loop.
I have strucuture array with 8 feilds. Please see below.
I am trying the following:
K>> alldata(ID=="864475047572713").Data
Unrecognized function or variable 'ID'.
This seems to be wrong, but what is the correct way of doing this?
K>> alldata
alldata =
1×8 struct array with fields:
ID
location
electrosensor_values
Data
K>> alldata(1)
ans =
struct with fields:
ID: "864475047690895"
location: "GPS12121"
electrosensor_values: [1×1 struct]
Data: [81603×12 timetable]
K>> alldata(2)
ans =
struct with fields:
ID: "864475047636435"
location: "GPS12121"
electrosensor_values: [1×1 struct]
Data: [79899×12 timetable]
K>> alldata(3)
ans =
struct with fields:
ID: "864475047572713"
location: "GPS12121"
electrosensor_values: [1×1 struct]
Data: [78759×12 timetable]
K>> alldata(4)
ans =
struct with fields:
ID: "864475047564256"
location: "GPS12121"
electrosensor_values: [1×1 struct]
Data: [84734×12 timetable]

채택된 답변

Matt J
Matt J 2022년 10월 25일
편집: Matt J 2022년 10월 25일
Perhaps this is what you meant?
somedata = alldata([alldata.ID]=="864475047572713")
  댓글 수: 1
Dharmesh Joshi
Dharmesh Joshi 2022년 10월 25일
Yes it seems [alldata.ID] solved the issue.
Thanks

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

추가 답변 (1개)

dpb
dpb 2022년 10월 25일
With the ID field being a string, probably the easiest syntax is simply
ID=="864475047572713";
idx=find(matches([alldata.ID],ID));
then idx is the index into the struct array of interest; all properties are then as
tDataID=alldata(idx).Data;
Now, in the specific case, you can access the table as normal thru the tDataID table variable.

카테고리

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

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by