Error in optimconstr() when used with single string vs array of string

2 ビュー (過去 30 日間)
Ravi Raj Saxena
Ravi Raj Saxena 2023 年 3 月 30 日
コメント済み: Aurele Turnes 2023 年 3 月 31 日
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?

採用された回答

Raghunathraju
Raghunathraju 2023 年 3 月 31 日
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 件のコメント
Ravi Raj Saxena
Ravi Raj Saxena 2023 年 3 月 31 日
編集済み: Ravi Raj Saxena 2023 年 3 月 31 日
But what if I have only 1 element?
Aurele Turnes
Aurele Turnes 2023 年 3 月 31 日
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.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSolver Outputs and Iterative Display についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by