Passing string as function argument

3 次查看(过去 30 天)
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

采纳的回答

David Young
David Young 2011-11-24
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 个评论
Sven
Sven 2011-11-24
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])
Maximilian
Maximilian 2011-11-24
done the entire thing within 15min. Spent hours trying to get it to work. Learn something new every day!

请先登录,再进行评论。

更多回答(2 个)

Andrei Caragea
Andrei Caragea 2011-11-24
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 个评论
Maximilian
Maximilian 2011-11-24
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?
Walter Roberson
Walter Roberson 2011-11-24
eval() is not at all recommended in this situation!

请先登录,再进行评论。


Maximilian
Maximilian 2011-11-24
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!

类别

Help CenterFile Exchange 中查找有关 Whos 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by