1

I am dealing with a rather large optimization problem (using MOSEK). To remind myself of the decision variables in this problem, I decided to try to store the variable names in a cell array in a struct, as desribed by MOSEK. However, I am unsure of how to do this. The relevant fragment of my code is listed below:

JiSet = 1:6; TiSet = [7 8]; nL = length(TiSet); 
P = struct;
P.names.var{1} = 'gamma_i(k+1)';    % my first constraint
P.names.var{2:1+nL,1} = cellstr(strcat('nu_',int2str(TiSet(:)),'(k+1)')); % a series of constraints of varying length

When I run the above code I get the following error:

The right hand side of this assignment has too few values to satisfy the left hand side.

However, if I enter strcat('nu_',int2str(TiSet(:)),'(k+1)') in my command window, it shows an ans variable that is a 2x1 cell (as desired). How do I assign the values in this cell to the 2:1+nL,1 entries in the P.names.var cell?

2 Answers 2

2

Try the same line with round brackets ( ), which are used to select sets of cells.

P.names.var(2:1+nL,1) = cellstr(strcat('nu_',int2str(TiSet(:)),'(k+1)'));

MyCell{1:2} refers to the literal contents of the cells.

MyCell = {10,24,-60};
MyCell{1:2}

ans =

    10


ans =

    24

MyCell(1:2) refers to a collection of cells.

MyCell(1:2)

ans = 

    [10]    [24]

Since cellstr(strcat('nu_',int2str(TiSet(:)),'(k+1)')); is a collection of cells, you need to assign it to a collection of cells, P.names.var(2:1+nL,1);

Sign up to request clarification or add additional context in comments.

Comments

2

The problem is in how you structure your assignment in the last line. The right-hand side creates a single value, a 2x1 cell array. However, the left-hand refers to two separate fields. This behavior is likely a result of the nature of cell-arrays, which may except a different data-type in each cell. Cell arrays are set up to assign whatever is on the right-hand side to a single cell on the left-hand side. You can fix this with a simple modification:

P.names.var(2:1+nL,1) = cellstr(strcat('nu_',int2str(TiSet(:)),'(k+1)'));

This utilizes "array indexing" as opposed to "cell indexing".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.