From: da288 on
Hi,

I realise this is a controversial topic containing both EVAL and Globals and having spent an entire day trying to solve it. Essentially I want to be able to dynamically generate global variables from an array. I can quite easily do:

eval('global x'); x = 5; which works fine

However, say my global variable name is in string vector 'names', initally containing just 1 element 'P'. Thus eval('names(1)'), for instance, displays the first desired variable name ('P'). However, the moment I change this to:

eval('global names(1)')
??? Error: Unbalanced or unexpected parenthesis or bracket.

The closest I can get is:

eval('global ', ['names(', num2str(1), ')'])

ans =

'P'

But as soon as I try to check for global variables, there are none present:

>> who global

I have also tried the following combinations, with no luck:

eval(['global ', 'names(', 1, ')'])
??? Error: Unbalanced or unexpected parenthesis or bracket.

eval(['global ','names(',1,')'])
??? Error: Unbalanced or unexpected parenthesis or bracket.

>> eval(['global names(',1,')'])
??? Error: Unbalanced or unexpected parenthesis or bracket.


The reason I am doing this is whole exercise is because JAVAADDPATH clears all live global variables (WHY won't Mathworks fix this GAPING error?), and so I can populate live globals into temporarly local variables and store the existing live global names and their values, run javaaddpath, and then repopulate the original globals with their original values. Thus the idea is that the original workspace is restored.

As you can tell I've tried a lot of different combinations to no avail. Any help would be very much appreciated!
From: us on
"da288 " <dahir288(a)googlemail.com> wrote in message <i2mnc5$oo9$1(a)fred.mathworks.com>...
> Hi,
>
> I realise this is a controversial topic containing both EVAL and Globals and having spent an entire day trying to solve it. Essentially I want to be able to dynamically generate global variables from an array. I can quite easily do:
>
> eval('global x'); x = 5; which works fine
>
> However, say my global variable name is in string vector 'names', initally containing just 1 element 'P'. Thus eval('names(1)'), for instance, displays the first desired variable name ('P'). However, the moment I change this to:
>
> eval('global names(1)')
> ??? Error: Unbalanced or unexpected parenthesis or bracket.
>
> The closest I can get is:
>
> eval('global ', ['names(', num2str(1), ')'])
> ans =
> 'P'

no... you're teasing yourself by inadvertently making a syntax mix-up...

% 1) you're using EVAL's second arg, which is active if EVAL encounters an error

eval('global','disp(''foo'')'); % <- TWO args...
% foo

% 2) the corrrect syntax would be

eval(['global ',['names(', num2str(1), ')']]); % <- ONE arg...
% which yields the same ...expected... error messsage...
%{
??? Error: Unbalanced or unexpected parenthesis or bracket.
%}

us
From: Walter Roberson on
da288 wrote:

> eval('global names(1)')

eval(['global ', names(1)]);
From: Oleg Komarov on
Walter Roberson <roberson(a)hushmail.com> wrote in message <TMB3o.49796$Ls1.25580(a)newsfe11.iad>...
> da288 wrote:
>
> > eval('global names(1)')
>
> eval(['global ', names(1)]);

I think you can still avoid eval:

names = {'a','b'};
global gVars
gVars.(names{1}) = 5;

Oleg
From: Jan Simon on
Dear da288,

> However, say my global variable name is in string vector 'names', initally containing just 1 element 'P'.

What do you mean by "string vector" Actually Matlab does not know such a data type. If you mean a "cell string", use curly braces to access the strings:
eval('global names{1}')

> The reason I am doing this is whole exercise is because JAVAADDPATH clears all live global variables (WHY won't Mathworks fix this GAPING error?), and so I can populate live globals into temporarly local variables and store the existing live global names and their values, run javaaddpath, and then repopulate the original globals with their original values. Thus the idea is that the original workspace is restored.

As you see, GLOBALs face you with troubles. Nobody told anything else ever.
Using a single struct as container for your GLOBALs, as Oleg suggests, is at least [number of variables]-times safer.

What are "live" globals?

Can somebody confirm that JAVAADDPATH clears global variables?

Jan
 |  Next  |  Last
Pages: 1 2
Prev: select features
Next: Error building Real-Time Workshop