How can I use a variable to create MATLAB elements? [duplicate]
Possible Duplicates: How to concatenate a number to a variable name in MATLAB? MATLAB: How can I use a variables value in another variables name?
I have the following code:
x = textread('/home/data/data.txt','%s')
for i=1:50
S=load(['/home/data/',x{i},'_file.mat'])
info_',x{i},' = strcat(S.info1, S.info2)
end
Of course, the last line (info_',x{i},' = strcat(S.info1, S.info2)
) doesn't work. It just doesn't seem to be possible to use a variable to create MATLAB elements. Is this right or am I just doing something wrong here? Is there an elegant workaround?
Do you want something like
eval( sprintf( 'info_%s = strcat( S.info1, S.info2 );', x{i} ) );
?
If so, could I discourage you from doing so, see: http://matlab.wikia.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F
Try the following:
eval(['info_x{' num2str(i) '}=strcat(S.info1, S.info2)']);
精彩评论