Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Adding variables dynamically?

Subject: Adding variables dynamically?

From: Danny

Date: 25 Jun, 2012 17:03:07

Message: 1 of 5

Hi, I'm currently implementing a MATLAB script into another program called Glyphworks, so this might not be the right place necessarily to ask this.
Essentially this program takes 'x' tests and runs them in a row, collating the data at the end. However, with the implemented MATLAB script it clears the variables for each loop and the program averages the tests as part of the calculations, so they must be retained in the script. To retain my data I used eval
eval(['Data_' num2str(i) '=Data;']);

However, I am met with this error:
MATLAB script failed with error: Error using glyphscript/process (line 38) Attempt to add "Data_1" to a static workspace. See <a href="matlab: helpview([docroot '/techdoc/matlab_prog/matlab_prog.map'],'adding_variables_dynamically')">MATLAB Programming, Restrictions on Assigning to Variables</a> for details.

Any ideas on how to retain my tests as they come, there could be any number of tests, that's why I loop variables in eval and give them Data_i name? I can save it to a cell and then load it from a .mat, but I still want to be able to isolate the a variable number of tests from the cell as different variable names.

Subject: Adding variables dynamically?

From: Matt J

Date: 25 Jun, 2012 17:36:07

Message: 2 of 5

"Danny " <dmccorm2@nd.edu> wrote in message <jsa5kb$ifg$1@newscl01ah.mathworks.com>...
> Hi, I'm currently implementing a MATLAB script into another program called Glyphworks, so this might not be the right place necessarily to ask this.
> Essentially this program takes 'x' tests and runs them in a row, collating the data at the end. However, with the implemented MATLAB script it clears the variables for each loop and the program averages the tests as part of the calculations, so they must be retained in the script. To retain my data I used eval
> eval(['Data_' num2str(i) '=Data;']);
>
> However, I am met with this error:
> MATLAB script failed with error: Error using glyphscript/process (line 38) Attempt to add "Data_1" to a static workspace. See <a href="matlab: helpview([docroot '/techdoc/matlab_prog/matlab_prog.map'],'adding_variables_dynamically')">MATLAB Programming, Restrictions on Assigning to Variables</a> for details.
>
> Any ideas on how to retain my tests as they come, there could be any number of tests, that's why I loop variables in eval and give them Data_i name? I can save it to a cell and then load it from a .mat, but I still want to be able to isolate the a variable number of tests from the cell as different variable names.
===================


I thought we established in your other recent thread that you shouldn't need to do this

http://www.mathworks.com/matlabcentral/newsreader/view_thread/321241

Why is it that you need your results stored in different variables?

In any case, the html link in the error message you got will explain that in certain MATLAB workspaces, like those of nested functions, you cannot create a variable non-explicitly, e.g. using EVAL.

It's bad regardless. Try running the function below to see just one reason why

function test

  eval('fft=[4 5 6];')
  out=fft(3),
end


The output that I get is the following, which is obviously wrong.

>> test

out =

     3

Subject: Adding variables dynamically?

From: Danny

Date: 25 Jun, 2012 17:50:07

Message: 3 of 5

How would I be better off? Storing in Cells/Matrices? And I've been getting the right answers, is it just not good habbit? I considered this a different case, because of the variable restriction, the other one was just matrix alignment.

Subject: Adding variables dynamically?

From: Matt J

Date: 25 Jun, 2012 18:00:08

Message: 4 of 5

"Danny " <dmccorm2@nd.edu> wrote in message <jsa8cf$1qk$1@newscl01ah.mathworks.com>...
>
> How would I be better off? Storing in Cells/Matrices?
==============

Yeah. What's the big difference for you between A{1}, A(:,:,1), or A1?


>And I've been getting the right answers, is it just not good habbit?
============

You've been lucky. As my example showed you, sometimes you get wrong answers.

Subject: Adding variables dynamically?

From: Danny

Date: 25 Jun, 2012 18:26:07

Message: 5 of 5


>
> Yeah. What's the big difference for you between A{1}, A(:,:,1), or A1?
>
>
> >And I've been getting the right answers, is it just not good habbit?
> ============
>
> You've been lucky. As my example showed you, sometimes you get wrong answers.
=============================================
Yeah, I guess A# was just how I envisioned the calculations, I have not done much programming (if any) with variable inputs, I'll stick to your method. I was unaware of the innacuracy of the eval function, but I see what you mean. I think Cells and nxmxp matrices are my best bet, thanks for your help again.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us