Hello everyone,
I am facing a problem using MATLABs assignin() function. First of all I have heard that it is not recommended by many users to use assignin. However, I think in this case it might be necessary.
What I need to do is read variable names as well as the corresponding values from an EXCEL user interface. The variable names are stored in cel array called "v", the values in cell array called "values". At the moment I am using the following code that works without any problems:
v = {'variableName1','variableName2'};
values = {10,20};
for n = 1:numel(v)
assignin('base',char(v(n)),values{n})
end
The problem is that some variables I want to assign values in are no single values but part of an array. For instance I might want to assign values in the variables "variableName1" and also in the second position of "variableName2". I tried to use the following code but it creates an error as "variableName2(2)" is not a valid variable name.
v = {'variableName1','variableName2(2)'};
values = {10,20};
for n = 1:numel(v)
assignin('base',char(v(n)),values{n})
end
Is there a way to fix this error?

3 commentaires

Stephen23
Stephen23 le 8 Mar 2019
"First of all I have heard that it is not recommended by many users to use assignin."
That is true. Read this to know some of the reasons why:
"However, I think in this case it might be necessary."
That is begging the question: if you design your code so that you force yourself into writing complex, slow, buggy code that magically assigns to variables in other workspaces, then that is what you force yourself to do. Simpler, neater, more efficient code would avoid doing that.
For example, the most simplest and most efficient way to pass variables between workspace is to pass them as input/output variables (and this is what experienced MATLAB users do too):
Lennart Vogt
Lennart Vogt le 8 Mar 2019
Hi Stephen,
Thank you for the background information. Some of it I have already read in this forum.
"That is begging the question"
I did not want to assume that using one of those "bad" functions is the only way to solve my problem.
However, I cannot imagine of another way to solve it. Maybe I should have explained more about my code...
I am not trying to create variables dynamically. I know that doing so is inefficient programming.
In my programm all variables are read from a main EXCEL sheet. Then there is a second EXCEL sheet where certain variables can be specified to be changing while the code is running.
For example I specify the variables a,b and c in EXCEL sheet 1. Then in EXCEL sheet 2 the user can select variables that are not constant but change from iteration to iteration of the program.
Those variables already exist in MATLAB as they were read from sheet 1 but now they get overwritten. However, not all of them get overwritten but only the ones specified in EXCEL sheet 2.
This flexibility is required and in my knowledge (I might be wrong here) I need assignin or evalin to realize it.
Stephen23
Stephen23 le 8 Mar 2019
"This flexibility is required and in my knowledge (I might be wrong here) I need assignin or evalin to realize it."
Nothing in code is "required", you always have a choice in how to approach a task. In this case you designed your data in such a way that you painted yourself into that corner: by using lots of separate variables you force yourself into writing slow, complex, buggy code. So without realizing you beg the question: "if I have lots of separate variables (which can be accessed only using slow and complex code), how can I access them?" Answer: using slow and complex code.
"I did not want to assume that using one of those "bad" functions is the only way to solve my problem."
You could trivially get the same flexibilty using a structure or a table, neither of which require magically accessing variable names: they would be more robust, easier to manage, easier to debug, and likely much more efficient:
Interfering in other workspaces is also inefficient, hard to debug, and is best avoided. The best way to pass data from one workspace to another is to pass it as input/output arguments.

Connectez-vous pour commenter.

 Réponse acceptée

Fangjun Jiang
Fangjun Jiang le 8 Mar 2019

1 vote

prepare the full assignment statement and use evalin().

3 commentaires

Lennart Vogt
Lennart Vogt le 8 Mar 2019
Could you please elaborate on that? I am not sure how tu use evalin() in this case.
v = {'variableName1','variableName2(2)'};
values = {10,20};
for n = 1:numel(v)
statement=sprintf('%s=%d;',v{n},values{n});
evalin('base',statement)
end
Lennart Vogt
Lennart Vogt le 8 Mar 2019
Works great. Thanks a lot!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Data Type Identification dans Centre d'aide et File Exchange

Produits

Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by