Passing variable to function with similar name

Hi, I have a workspace with variable EURpippo, EURpluto, EURpaperino. These variables are dynamics, in the sense that they can change in number and second part of the name. Is it possible to create a function that: 1) read from the workspace EUR*** 2) upload all variable that in the name start with EUR?
Thanks
Regards
Massimo

1 comentario

Stephen23
Stephen23 el 26 de Jul. de 2017
Editada: Stephen23 el 26 de Jul. de 2017
"Is it possible to create a function that: 1) read from the workspace EUR*** 2) upload all variable that in the name start with EUR?"
Is it possible? Yes.
Is it a good way to write code? No.
Magically making all variables jump from workspace to another workspace totally defeats the whole point of independent workspaces: the fact that they are independent.
And then there are all the problems with magically creating variables:
Much better would be to not magically create variables like that. Putting meta-data into the names means that your code is going to be slow and complicated. Remember that meta-data is data, which means that it should be stored in a variable, not in the variable name. Then your code would be simpler and much more efficient.

Iniciar sesión para comentar.

Respuestas (3)

Guillaume
Guillaume el 26 de Jul. de 2017
Rather than using excel to talk to matlab (through the spreadsheet link), I'd use matlab to pull the data from excel. This would be more flexibe and doesn't require paying for a toolbox.
With what you have now, the best thing to do is to fix the mess created by MLPutRanges to put everything into one variable. This has to be executed as a script (not a function) as it needs access to the main workspace:
varnames = who('EUR*');
varvalues = cellfun(@eval, varnames, 'UniformOutput', false);
varpairs = [varnames, varvalues]';
eur = struct(varpairs{:});
You can then use the eur structure to pass all your variables at once to functions. fieldname(eur) gives you the list of all the fields eur.
Jan
Jan el 26 de Jul. de 2017
Editada: Jan el 26 de Jul. de 2017
Don't do this. To juggle with a pile of variables having a magic key in their name is a really bad idea. Even extracting variables from another workspace magically is a bad programming practice, because it cannot be seen in the source code, where which variable is coming from. Store the data in a struct instead:
EUR.pippo = rand(1,17);
EUR.pluto = 'hello';
EUR.paperino = @myFcn;
Then you can start your code by providing EUR as input argument. This is clean and efficient.
Read Stephen's valuable comment carefully.

6 comentarios

Stephen23
Stephen23 el 26 de Jul. de 2017
Massimo Laspia's "Answer" moved here:
Thanks, but if these variables are dynamics and they are uploaded from an Excel spreedsheet via VBA code, how can I uoload them as a struct? To be clear: I have an Excel file with different spreadsheet, one is called "Curve", inside it, I add dynamically interest rate curves in different currency. Then I need to upload them separately and then compact them into a single variable to manage them into a mat code.
Massimo
Stephen23
Stephen23 el 26 de Jul. de 2017
@Massimo Laspia: how do you get these variables into the MATLAB workspace?
Massimo Laspia
Massimo Laspia el 26 de Jul. de 2017
Editada: Massimo Laspia el 26 de Jul. de 2017
with a VBA code that reads if spreadsheet "curve" has one or more curves (Matrix Nx2, first col data, second col rate) these variable are uploaded in matlab with different name depending of the currency of the curve. using MLPutRanges
Stephen23
Stephen23 el 26 de Jul. de 2017
@Massimo Laspia: What does "this variable are uploaded in matlab" mean exactly? I repeat my question once more: how do you get these variables into the MATLAB workspace?
Guillaume
Guillaume el 26 de Jul. de 2017
@Stephen,
Unfortunately, it looks like the variables are created thus by MathWorks' MLPutRanges VBA function from the spreadsheet link toolbox!
Stephen23
Stephen23 el 26 de Jul. de 2017
@Guillaume: aaah, thank you. Indeed, that is rather unfortunate.

Iniciar sesión para comentar.

Massimo Laspia
Massimo Laspia el 26 de Jul. de 2017

0 votos

ok thanks, so I should launch scripts from vba, right? This will generate automatically strut eur in matlab main workspace.
Massimo

Categorías

Más información sobre Data Import from MATLAB en Centro de ayuda y File Exchange.

Preguntada:

el 26 de Jul. de 2017

Respondida:

el 26 de Jul. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by