How to generate variables from structure field names in Matlab?

Let's say I have structural data in the workspace as follows

data.a={'1','2'};
data.b=[1,2,3];
data.c='hello';

I need to have the following variables in the workspace:

a={'1','2'};
b=[1,2,3];
c='hello';

Please tell me how to move forward with this?

Why do I need this (in the case of a better alternative to achieve my requirement):

I have about 140 .mat files. Now I need to replace the line in all these .mat files. This row can be part of an array of strings, an array of cells, or a single variable. I run a loop for all these mat files and load them like this:

tempLoad=load('filename.mat');

tempLoad, 'filename.mat'. . 'filename.mat' .

save('filename.mat','tempLoad') save('filename.mat'), .

tempLoad , tempLoad save .

+4
3

tempLoad 'filename.mat' '-struct':

save( 'filename.mat', '-struct', 'tempLoad' );

. doc save.

matfile / .

+3

-struct arg:

matData = load ( 'filename.mat' );

save ( 'filename2.mat', '-struct', 'matData' )

. , , , ...

+1

I think other answers may help your problem. If you still want to keep the new variables from the fields, you can use

f = fieldnames(data);

for ii = 1:numel(f)
    eval([f{ii} ' = data.' f{ii}]);
end

Please note that if you use this, there may be a better way to think about a problem while trying to solve it.

0
source

All Articles