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 some 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:
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
extfuncreturns a vector/array/matrix (formean; note that you do not recordvariancethis way), but your assigmentm(i)= ...can take only scalar values. Ifmeanis a vector, trym(i,:)=....However, I would rather question your general approach of dealing with strings and
evalhere. 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:
If you prefer
sprintfto char array concatenations for some reason, tryinstead. I remain unconvinced that using
evaland different variable names is a more elegant solution to your problem than collectingeV50,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.