MATLAB Answers

Can you replace names in struct?

27 views (last 30 days)
Alexa Z
Alexa Z on 15 Dec 2020
Commented: J. Alex Lee on 15 Dec 2020
Hello.
Can you replace names in struct? We have 35 rows in a struct with following names
EMG_HRstruct(1).data
EMG_HRstruct(2).data
untill EMG_HRstruct(35).data
TD11 has three files in row 1,2,3 and TD12 has three files in row 4,5,6 and so on.
We have to refer to TD11 for example but it is not in the name of our values from the struct.
CAN WE CHANGE THE NAMES? SO THAT WE CAN REFER TO THE FIRST THREE ROWS FOR TD11?
Thanks.
  5 Comments
Alexa Z
Alexa Z on 15 Dec 2020
can we change the yellow name?

Sign in to comment.

Accepted Answer

J. Alex Lee
J. Alex Lee on 15 Dec 2020
Going out on a limb since as Matt J points out it's not clear what exactly everything is, but...
I think what you need is metadata.
If you don't want to change your basic data type, maybe create an intermediary structure lookup to translate names like TD11 into a list of rows
lookup.TD11 = [1,2,3]
lookup.TD12 = [4,5,6]
...
Then you can access the rows like
target = "TD11"
EMG_HRstruct(loopup.(target)).data
which I guess you will have to somehow cat() together if you don't want the data for each row to be spit out separately.
If you can alter your data structure, maybe consider tabular data with a column for the file group
row|filegrp|data
---+-------+----
1 | TD11 | ...
2 | TD11 | ...
3 | TD11 | ...
4 | TD12 | ...
5 | TD12 | ...
6 | TD12 | ...
7 | TD13 | ...
  3 Comments
J. Alex Lee
J. Alex Lee on 15 Dec 2020
Yes you can, but at that point I would use a table instead of a struct array if you want to be able to easily index. Of course you can append a new field to EMG_HRstruct so that you can do
EMG_HRstruct(1).Group = "TD11"
EMG_HRstruct(2).Group = "TD11"
EMG_HRstruct(3).Group = "TD11"
EMG_HRstruct(4).Group = "TD12"
EMG_HRstruct(5).Group = "TD12"
EMG_HRstruct(6).Group = "TD12"
But how will you refer to the appropriate rows by specifying 'TD11'? Unless I am missing something, you'd anyway need to create a temporary variable to identify the appropriate "rows", e.g.
idx = [EMG_HRstruct.Group]=="TD11";
subdata = [EMG_HRstruct(idx).data]; % assuming data can be cat()-ed
or whatever

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by