loading from *.mat file directly into a structure
Afficher commentaires plus anciens
OK, I'm taking your suggestions to heart...eliminate the "eval"s and "poofing" variables into my workspace in my code. But I have a question:
Each wkrspc is saved to its own *.mat file with a single variable (a 6x6 cell array) with the structure shown above. If I want to replace the code:
load([wrkspcs{iwrk},'.mat']) %creates variables in workspace matching the file name
(which "poofs" the variable 'ZLH_151212_WrkSpc' into the workspace) with the preferred "load into structure" replacement statement
>> tmp=load([wrkspcs{1},'.mat'])
tmp =
struct with fields:
ZLH_151212_WrkSpc: {6×6 cell}
it works fine. I can then copy the cell array from the 'tmp' struct to myStruct
>> myStruct.(wrkspcs{1})=tmp.(wrkspcs{1})
myStruct =
struct with fields:
ZLH_151210_WrkSpc: {6×6 cell}
But how can I do the load directly into 'myStruct' struct? When I try:
>> myStruct.(wrkspcs{1})=load([wrkspcs{1},'.mat'])
myStruct =
struct with fields:
ZLH_151210_WrkSpc: [1×1 struct]
>> myStruct.ZLH_151210_WrkSpc
ans =
struct with fields:
ZLH_151210_WrkSpc: {6×6 cell}
>> myStruct.ZLH_151210_WrkSpc.ZLH_151210_WrkSpc
ans =
6×6 cell array
{1×1 struct} {[1]} {'16557'} {'1210'} {'zlh_1a'} {'ZLH_151210'}
{1×1 struct} {[1]} {'16676'} {'1210'} {'zlh_1b'} {'ZLH_151210'}
{1×1 struct} {[2]} {'16557'} {'1213'} {'zlh_2a'} {'ZLH_151210'}
{1×1 struct} {[2]} {'16676'} {'1213'} {'zlh_2b'} {'ZLH_151210'}
{1×1 struct} {[3]} {'16557'} {'1216'} {'zlh_3a'} {'ZLH_151210'}
{1×1 struct} {[3]} {'16676'} {'1216'} {'zlh_3b'} {'ZLH_151210'}
and my 6x6 cell array gets buried 2 levels deep in 'myStruct'. Now I have no idea how to directly get the 6x6 cell array to be "loaded" directly as the first level field in 'myStruct' -- same as the result I get when going through a 'tmp' struct as shown above. Specifying the variable name in the 'load' statement makes no difference. Also, specifying the 'myStruct' field directly in the load statement makes no difference.
Now I did read the "load()" documentation pretty carefully and it does suggest the result of the "s = load()" will always be a structure, so I guess my observed behavior is correct -- but it seems like there should be a way to avoid the "struct.struct.value" result when the LHS of the assignment is already a structure.
Again, I need some help understanding this behavior and any workaround to avoid going thru loading to a temporary structure and copying the desired field to my structure variable.
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Structures dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!