assignment between unlike types is not allowed. in matlab

Assigning between unlike types such as matrix and cell is not allowed in Matlab. The error message that you may encounter is similar to the following:

main.m
Unable to perform assignment between dissimilar types.
55 chars
2 lines

In Matlab, each variable can be of a particular type, which can be either:

  1. Numeric, such as double, single, and int8.
  2. Logical, with values of true or false.
  3. Character, representing a single text character.
  4. String, representing a sequence of text characters.

To assign values between unlike types in Matlab, you need to convert them to compatible types first. For example, if you have a matrix and want to assign it to a cell array, you need to convert the matrix to a cell array first:

main.m
% define a matrix
A = [1 2; 3 4];

% convert the matrix to a cell array
C = num2cell(A);

% assign the cell array to another variable
B = C;
141 chars
9 lines

Alternatively, if you have a cell array and want to assign it to a matrix, you can convert the cell array to a matrix first. However, this only works if all elements in the cell array are numeric:

main.m
% define a cell array
C = {1, 2, 3; 4, 5, 6};

% convert the cell array to a matrix
A = cell2mat(C);

% assign the matrix to another variable
B = A;
149 chars
9 lines

Note that in some cases, you may also need to reshape or transpose the variables to make them compatible before assignment.

related categories

gistlibby LogSnag