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
0 个评论
采纳的回答
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
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])
更多回答(2 个)
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Whos 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!