Extract data from nested structure and save in dynamically named cell
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
i want to export my data out of a nested structure into individual cells for exporting to use in another program. my data look as follows:
stucture a has 12 strucutes (subjects) s1,s2,..s12. in each of these strucutes there are 2 strucutres (muscles) m1 and m2, which again contain structures (condition) c1 to c3 which contains the actual data (timeseries).
what i want to do is get cells that are dynamically named
s1m1c1 --- s12m2c3
which then should contain the data in the lowest level of the nested strucutre.
I already fail in generating the variable names dynamically.
3 commentaires
Stephen23
le 18 Fév 2017
Modifié(e) : Stephen23
le 18 Fév 2017
"I think I know what the problems might be"
Probably not if you are still considering using them. All expert programmers avoid using dynamically named variables, because they really do know how it makes code slow, buggy, obfuscated, and hard to debug:
Also note that dynamically named variables are just as bad in Python as they are in MATLAB, so you double your trouble!
Réponses (2)
Stephen23
le 18 Fév 2017
Modifié(e) : Stephen23
le 18 Fév 2017
This is easy without dynamically named variables. Here is an example of how to use scipy to access a non-scalar structure saved in a mat file, including some nested structures. First create the (nested) structures in MATLAB and save in a mat file:
>> S(1).muscles = struct('data',1:3,'time',0:2);
>> S(2).muscles = struct('data',4:6,'time',1:3);
>> S(3).muscles = struct('data',7:9,'time',2:4);
>> S(1).name = 'anna';
>> S(2).name = 'bob';
>> S(3).name = 'cathy';
>> save('test.mat','S')
And load it with scipy and access the data (I used Ipython):
import scipy.io as sio
obj = sio.loadmat('test.mat')
S = obj["S"][0]
S[0]["name"][0]
Out[41]: u'anna'
S[1]["name"][0]
Out[42]: u'bob'
S[2]["name"][0]
Out[43]: u'cathy'
S[0]["muscles"]["data"][0][0]
Out[44]: array([[1, 2, 3]], dtype=uint8)
S[0]["muscles"]["time"][0][0]
Out[45]: array([[0, 1, 2]], dtype=uint8)
etc.
Summary: dynamic variables are not required. Existing tools work better: faster, neater, easier, clearer, and much more reliable than any hack code any beginner could come up with using dynamic variables.
0 commentaires
Walter Roberson
le 18 Fév 2017
In Ye Olde Days, in programming languages that did not have structures or pointers, the tactic was this:
- Create one variable for each different data type
- store all of the entries with that same data type as consecutive entries in those variables
- have another variable which records the indices into the above variables
The above was easiest when the structures were the same form for each entry, but could also be used for variant structures (you just had to keep a record of which variant was being used.)
In some cases you do not even need to store the indices, if the number of consecutive entries used is the same in each case.
When you set up a situation like this then all you have to be able to do for the interface is to pass one variable per simple data type (e.g., double() or uint16()) along with the index information. If the target language supports structures then you can use code in the target language to assemble the entries by reading off the index information and using that to pull out entries from simple data.
0 commentaires
Voir également
Catégories
En savoir plus sur Variables 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!