Error in optimconstr() when used with single string vs array of string
    2 views (last 30 days)
  
       Show older comments
    
    Ravi Raj Saxena
 on 30 Mar 2023
  
    
    
    
    
    Commented: Aurele Turnes
    
 on 31 Mar 2023
            I'm working on  MATLAB Version: 9.12.0.1884302 (R2022a) with Optimization Toolbox (9.3).
I  have created 2 sets flight_name_set & Edges of type string
optimconstr gives the below error whenever the set is of single value but not on multiple values.
Error using optimconstr
Dimensions specifications must be a positive integer, string array, or a
cell array of character vectors.
For e.g
When Edges has 2 values Then there is no error.
flight_name_set = 
  1×5 string array
    "Small-tt-1"    "Medium-yy-2"    "Medium-rr-3"    "Jumbo-mm-4"    "Jumbo-zz-5"
Edges = 
1×2 string array
"W-Y"    "S-Y"
optimconstr(Edges, flight_name_set, flight_name_set)
ans = 
  2×5×5 OptimizationConstraint array with properties:
    IndexNames: {{1×2 cell}  {1×5 cell}  {1×5 cell}}
     Variables: [1×1 struct] containing 0 OptimizationVariables
  See constraint formulation with show.
but when Edges have 1 value there is error
flight_name_set = 
  1×5 string array
    "Small-tt-1"    "Medium-yy-2"    "Medium-rr-3"    "Jumbo-mm-4"    "Jumbo-zz-5"
Edges = 
    "W-Y"
optimconstr(Edges, flight_name_set, flight_name_set)
Error using optimconstr
Dimensions specifications must be a positive integer, string array, or a cell array of character vectors.
With hit and trial I have to put a check on length and then use {Edges{:}} if there is only 1 value. Is there a way to use genralise statements?
0 Comments
Accepted Answer
  Raghunathraju
    
 on 31 Mar 2023
        Hi Raviraj,
The 'optimconstr' function cannot have a single argument as string, The input arguments can be a string array, a cell array, or it can be a positive integer .If you want to use a string as an input argument then it must be an array of strings.
2 Comments
  Aurele Turnes
    
 on 31 Mar 2023
				When you have a single element, you should use a cell array of character vectors like this:
optimconstr(cellstr(Edges), flight_name_set, flight_name_set)
This is because optimconstr and optimexpr go hand-in-hand with optimvar, which accepts string inputs and name-value arguments. For this reason, when writing:
optimvar("x", "Typo", "integer")
It is impossible to know if this is a typo and means:
optimvar("x", "Type", "integer")
or if this means:
optimvar("x", {'Typo'}, {'integer'})
To avoid silently returning the wrong answer, we error and require to pass a cell array for a single string input. Optimexpr and optimconstr behave similarly for consistency.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

