Error in setting UpperBound in optimvar

12 views (last 30 days)
I'm trying to implement this example:
when I get to this line:
power = optimvar('power',nHours,plants,'LowerBound',0,'UpperBound',maxGenConst);
I get the following error message: " _Error using optimvar (line 118) Error setting property 'UpperBound' of class 'optim.problemdef.OptimizationVariable': Value must be numeric."
The UpperBound variable maxGenConst is a 24x4 table containing numerical values. What is the problem here?
  4 Comments
George B
George B on 28 Sep 2018
But in the example, it looks like their input data is in table format, and they didn't convert maxGenConst into double. Am I missing something here?
Micah Mungal
Micah Mungal on 21 May 2020
Do you have the code for the Script 9 example. I cant find it.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 28 Sep 2018
plantProps is a table() object, but
MaxGenerationLevel = plantProps{5,:};
which extracts row 5 of plantProps table into numeric form.
Then,
maxGenConst = repmat(MaxGenerationLevel,nHours,1);
replicates that numeric row into multiple rows, still giving you numeric form.
So by the time you are at
power = optimvar('power',nHours,plants,'LowerBound',0,'UpperBound',maxGenConst);
maxGenConst is numeric, not a table object.
  2 Comments
George B
George B on 29 Sep 2018
Well, in my case,
FuelCost, ..., MinimumDownTime
all turn into cell arrays, further calculations result in errors, such as:
Error using *
Conversion to
optim.problemdef.OptimizationExpression from
cell is not possible.
Error in Main (line 53)
powerCost = sum(power*FuelCost',1);
the code runs smoothly when I convert those variables into double matrices, as follows:
FuelCost = cell2mat(plantProps{1,:});
This isn't a big deal, I just want to get a better understanding of matrix/cell/table operations. Also, is table object different from table array, or is it the same thing?
Walter Roberson
Walter Roberson on 29 Sep 2018
Why do those items turn into cell arrays? Are you doing a readtable() from a file? If so then you probably need to adjust the readtable(), perhaps in connection with DetectImportOptions if you have a new enough version for that.
I use the term "table object" to emphasize that I am referring to the MATLAB "table" data type, not just data that happens to be arranged in a 2D presentation and therefore might be commonly called a "table".
Likewise, I refer to "string object" to emphasize when I am referring to the MATLAB "string" data type rather than referring to other things that might commonly be called strings. Historically, character row vectors were commonly called strings, which you can see in function names such as strcmp() ["string compare"] and cellstr() ["cell string"]
Table objects are a structured data type. Within each column of a table object, all values must be the same data type, but the different columns can be different data types. If it is necessary to have a mix of data types within one column, then the entries in the column must be stored in a cell array. For example if there were a text header that was stored along with numeric values then because the text would be a character vector or string object scalar, a different data type than the numeric values, the column would have to be represented as a cell array. But if the creation of the table object were to be such that the character vector or string scalar never got there, then all of the entries in the column could be numeric and cell2mat() would not be needed.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!