Passing string as function argument

Hey. I am having a little difficulty with processing a large batch of data. I have approximately 1700 independent data points in my workspace. I would like to scale each of those with respect to a factor. Each variable is 1024*2 with the name something like spec100_0.
If I use a = who to list all variables, I can access each string by a{i,1} = (variablename)
Is there a way to use a{i,1} with a counter to pass all variables through a function? At the moment when i do that I only get the string but not the associated variable.
Thanks for any help.
Max

 Accepted Answer

David Young
David Young on 24 Nov 2011
It's a bad idea to store your data in 1700 different variables: you'll find it is inefficient, and you already see how awkward it makes your code. It's far far better to use an array with 1700 elements. See this article.

10 Comments

Agreed. I don't think I've ever seen a case where you would want to do it by making a ton of variables spec1, spec2, spec3 etc...
But if you still just want to know how you might do it, there are some simple ways:
%Multiply all these by "factor"
spec1 = 3;
spec2 = -6;
spec3 = 7.2;
factor = 10;
VARS = who('spec*');
S = strcat(VARS, '=factor*',VARS,';')';
eval([S{:}])
Do not let EVAL pollute the symbol lookup table for variables and functions! The dynamic creation of variables is prone to errors, hard to debug and slows down Matlab very hard. +1
See the problem is. Each of these data points contains another 1024*2 points so the array would be 1700*1024*2 which is just as awkward.
Basically each array is a point in space with a spectrum associated in the array. Need to do some baseline fitting/ adjusting before I can condense it down.
Thx for your quick answers!
1700*1024*2 is actually quite reasonable in terms of data size. I can guarantee that it will take more memory, be harder to debug, and be more hassle to store strings pointing to each variable and run all your commands through eval() than it would to store things in arrays. I see hope that we can convince you :)
Maximilian: Really, it is not as awkward to use an array. If you use lots of variables, you've got the problem of having to construct a string for each one, maybe keep those strings in another array, and use eval to get at the values. The code is difficult to write, and what goes on "under the hood" is horrendous! With an array, you could have a 1700*1024*2 array, as Sven says that's no problem, or alternatively you could use a 1700-element cell array, with each cell holding the matrix that would have been the value of one of your variables.
Okay. How would I go about creating that 1700 element cell array with the matrices in each one. That does have me interested. Especially as I can imagine the code being far easier. Must admint so far I have mainly used matlab with
array's rather than matrices.
myCell = cell(1700,1);
for i = 1:length(myCell)
myCell{i} = rand(1024,2);
end
--- OR, better (in my opinion) ---
myData = rand(1024, 2, 1700);
Need to scale by a factor? Easy:
myData * 3
Thx Sven! the top one is more appropriate as I need to scale each matrix column independently. This does make live much easier just need to keep track of positions now.
Need to scale by two different amounts for the two columns? Also easy:
[myData(:,1,:)*5 myData(:,2,:)*10]
There's also a slightly cleaner version with a little more advanced syntax:
bsxfun(@times, myData, [5 10])
done the entire thing within 15min. Spent hours trying to get it to work. Learn something new every day!

Sign in to comment.

More Answers (2)

Andrei Caragea
Andrei Caragea on 24 Nov 2011
Yes you can, with the eval function. Let me give you an example. Say you havein your workspace 3 variables: A, B, S, where A and B are doubles (matrices with numbers basically) and S is a cell like S={'A';'B'}. Then S(1)='A' and eval(S{1})=content of A (for example [1 2;3 4]). So now if f is your function, just do for i=1:2 f(eval(S{i})) end.

2 Comments

Yup. That's what I did but how do I manage to pass back the variable name stored in S so that the content of A gets overwritten?
eval() is not at all recommended in this situation!

Sign in to comment.

Maximilian
Maximilian on 24 Nov 2011
Exactly what I wanted well partially.
Any ideas on how to pass back the string as your outputfilename. I don't want to create more variables/ manually rename each and everyone!
Thx for the help!

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!