Render 1D column of OptimizationVariable/OptimizationExpression objects to string?
조회 수: 2(최근 30일)
표시 이전 댓글
In Matlab 2019a, I have a table wherein a column `IFpvfd` is an `OptimizationVariable` array:
myTable = table( [1:3]' , ...
optimvar( 'IFpvfd' , 3 , 1 , 'Type','integer' , ...
'LowerBound',0 , 'UpperBound',1 ) , ...
'VariableNames' , {'RowNbr' 'IFpvfd'} )
myTable = 3×2 table
RowNbr IFpvfd
______ ___________________________________________
1 [1x1 optim.problemdef.OptimizationVariable]
2 [1x1 optim.problemdef.OptimizationVariable]
3 [1x1 optim.problemdef.OptimizationVariable]
showvar( myTable.IFpvfd )
[ IFpvfd(1) ]
[ IFpvfd(2) ]
[ IFpvfd(3) ]
I want to add a column `IFpvfd2` that shows the `OptimizationVariable` more readably, either as a string array or cell array of character arrays:
RowNbr IFpvfd IFpvfd2
______ ___________________________________________ _________
1 [1x1 optim.problemdef.OptimizationVariable] IFpvfd(1)
2 [1x1 optim.problemdef.OptimizationVariable] IFpvfd(2)
3 [1x1 optim.problemdef.OptimizationVariable] IFpvfd(3)
I was hoping that there was a function to do this using arrayfun:
myTable.IFpvfd2 = arrayfun( @SomeFunc , myTable.IFpvfd )
For `SomeFunc`, I don't think I can use an anonymous function based on `sprintf` because the formatting string only accepts primitive data:
@(x) sprintf( 'Some Formatting String' , x )
I can't use `showvar` or `writevar`, as they send the output to either console and a text file without providing a return argument.
댓글 수: 0
채택된 답변
Walter Roberson
2021년 4월 22일
myTable = table( [1:3]' , ...
optimvar( 'IFpvfd' , 3 , 1 , 'Type','integer' , ...
'LowerBound',0 , 'UpperBound',1 ) , ...
'VariableNames' , {'RowNbr' 'IFpvfd'} )
myTable.IFpvfd2 = rowfun( @SomeFunc , myTable, 'InputVariables', 'IFpvfd' )
function S = SomeFunc(OV)
warnstate = warning('off', 'MATLAB:structOnObject');
OVS = struct(OV);
warning(warnstate);
S = categorical(sprintf("%s(%d)", OVS.Name, OVS.OptimExprImpl.getVarIdx));
end
Note: it did not work to create SomeFunc as an anonymous function: for reasons I do not understand, MATLAB claimed there was no function with that name that was applicable to that datatype.
추가 답변(1개)
참고 항목
범주
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!