Search code examples
matlabsymbolic-math

How do I know if an element in a syms vector is assigned (MATLAB)?


Define a syms vector

f = sym('f', [1 100]);

Define a syms variable x

syms x

The elements in vector f may be accessed and assigned, e.g.,

f(i) = x

Given any k, then how do I know if f(k) is assigned?


Solution

  • Short answer

    Let k be the index of the entry of f to be inspected. Then

    isAssigned = ~isempty(whos(char(f(k))));
    

    is true (or 1) if the k-th entry of f has been assigned and false (or 0) otherwise.

    Long answer

    From the documentation (boldface added)

    A = sym('a',[m,n]) creates an m-by-n symbolic matrix filled with automatically generated elements. The generated elements do not appear in the MATLAB workspace.

    For example,

    >> clear all
    >> f = sym('f', [1 10])
    >> f =
    [ f1, f2, f3, f4, f5, f6, f7, f8, f9, f10]
    >> whos
      Name      Size            Bytes  Class    Attributes
    
      f         1x10              112  sym
    

    which indeed shows that f1, f2 etc don't appear in the workspace. However, if you then assign

    >> syms x;
    >> f(3) = x
    f =
    [ f1, f2, x, f4, f5, f6, f7, f8, f9, f10]
    

    the variable x of course does appear in the workspace:

    >> whos
      Name      Size            Bytes  Class    Attributes
    
      f         1x10              112  sym                
      x         1x1               112  sym   
    

    So, a way to check if a particular entry of f has been assigned is to check for its existence in the workspace using the functional form of whos. Compare

    >> whos('f2') %// produces no output because no variable f2 exists in the workspace
    

    and

    >> whos('x') %// produces output because variable x exists in the workspace
      Name      Size            Bytes  Class    Attributes
    
      x         1x1               112  sym                
    

    Given the index k of the entry of f to be inspected, you can automatically generate the corresponding string ('f2' or 'x' in the above example) using char(f(k)):

    >> k = 2;
    >> char(f(k))
    ans =
    f2
    
    >> k = 3;
    >> char(f(k))
    ans =
    x    
    

    It only remains to assign the output of whos(char(f(k))) to a variable, which will be empty if f(k) has not been assigned, and non-empty if it has been assigned:

    >> k = 2;
    >> t = whos(char(f(k)))
    t = 
    0x1 struct array with fields:
        name
        size
        bytes
        class
        global
        sparse
        complex
        nesting
        persistent
    
    >> k = 3;
    >> t = whos(char(f(k)))
    t = 
              name: 'x'
              size: [1 1]
             bytes: 112
             class: 'sym'
            global: 0
            sparse: 0
           complex: 0
           nesting: [1x1 struct]
        persistent: 0
    

    Thus, applying ~isempty to this t produces true (1) if the k-th entry of f has been assigned and false (0) otherwise:

    >> k = 2;
    >> isAssigned = ~isempty(whos(char(f(k))))
    isAssigned =
         0
    
    >> k = 3;
    >> isAssigned = ~isempty(whos(char(f(k))))
    isAssigned =
         1