开发者

Processing string of 3D variables in MATLAB

I have been trying to write a script to process a string of 3D variables, eV50, eV60, eV70, etc. (meaning extracted value from the 1950, 1960, 1970 and so on. These 3D variables have the size of 31x145x192.) into a string of 3D output, that is, m1, m2, m3, etc. (dimension of 1x145x192).

This might be pretty straightforward to some of you, but I have been pulling my hair out in the last 24 hours trying to get this working. I tried these two approaches by using loops and EVAL, but I have troubles understanding the '' bracket in the expression, so I guess this is why I couldn't balance the equation correctly.

*X is the data set eV50, eV60, eV70, etc. (meaning extracted value from the 1950, 1960, 1970, etc.)

Basically I'm trying to turn this script into a loop or s开发者_运维百科ome sort of similar:

'[ m1 v1 ] = extfunc ( eV50 ) ;

[ m2 v2 ] = extfunc ( eV60 ) ;

[ m3 v3 ] = extfunc ( eV70 ) ;

[ m4 v4 ] = extfunc ( eV80 ) ;

[ m5 v5 ] = extfunc ( eV90 ) ;

[ m6 v6 ] = extfunc ( eV100 ) ;

and so on... '


Approach 1:

'[mean vars] = eval(['extfunc( sprintf(',eV%d', (50:10:80)') ')']); '


Approach 2:

*I have renamed eV50, eV60, eV70 into eV1, eV2, eV3 and so on...

' for i=1:6

m(i)=extfunc_h(sprintf('eV%d', i));

end '

Usage of extfunc :-

[mean variance] = extfunc(eV50)

In approach 1, I've unbalanced equations and approach 2, MATLAB returned the error message

??? Index exceeds matrix dimensions.'



One problem with approach 1 is that each ' starts or ends a string, but you want some of them to be part of the string that is supposed to be produced. You need two ' for this:

[mean vars] = eval(['extfunc( sprintf('',eV%d'', (50:10:80)'') '')']);

This will still not do what you want. If you want a string to be produced for each element of 50:10:80, you will need a loop.

The problem with the second approach is probably that your extfunc returns a vector/array/matrix (for mean; note that you do not record variance this way), but your assigment m(i)= ... can take only scalar values. If mean is a vector, try m(i,:)=....

However, I would rather question your general approach of dealing with strings and eval here. If it is so easy to rename the variables, why not put them into a cell array or (n+1)-dimensional matrix and have your functions work on actual variable content instead of variable name strings?

[edit: misleading "of" -> "or" before "(n+1)-dimensional matrix"]

Edit in response to edited question: Although the purpose of SO is not to let other people do your work, what you want seems to be quite straightforward:

year = 50:10:100;
for i=1:length(year)
    cmdstr = ['[m' int2str(i) ' v' int2str(i) '] = extfunc(eV' int2str(year(i)) ');'];
    eval(cmdstr);
end

If you prefer sprintf to char array concatenations for some reason, try

cmdstr = sprintf('[m%d v%d]=extfunc(eV%d);',i,i,year(i))

instead. I remain unconvinced that using eval and different variable names is a more elegant solution to your problem than collecting eV50, eV60, etc. in one matrix, e.g. eV(:,:,:,1) = eV50; eV(:,:,:,2) = eV60; etc. or a cell array and working on that one, unless your original data source is indeed a MATLAB binary file with variables named thusly.


It is not quite clear what should be the input to extfunc (what is X) - is it a single variable, i.e ev50, or a list of variables?

Anyway, the problem in the 1st approach is that the sprintf produces the following string:

,eV50,eV60,eV70,eV80

so you have an extra , at the begging. In addition, it fits only if extfunc gets a list of variables as an input. Otherwise, you should reformat your string.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜