Borrar filtros
Borrar filtros

Passing string as function argument

7 visualizaciones (últimos 30 días)
Maximilian
Maximilian el 24 de Nov. de 2011
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

Respuesta aceptada

David Young
David Young el 24 de Nov. de 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 comentarios
Sven
Sven el 24 de Nov. de 2011
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 el 24 de Nov. de 2011
done the entire thing within 15min. Spent hours trying to get it to work. Learn something new every day!

Iniciar sesión para comentar.

Más respuestas (2)

Andrei Caragea
Andrei Caragea el 24 de Nov. de 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 comentarios
Maximilian
Maximilian el 24 de Nov. de 2011
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 el 24 de Nov. de 2011
eval() is not at all recommended in this situation!

Iniciar sesión para comentar.


Maximilian
Maximilian el 24 de Nov. de 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!

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by