# Structure Subscripted Assignment Dimension Mismatch In Matlab

t2 is a cable all of whose variables are cell arrays. That's almost certainly not what you want.

Assigning one row at a time is sometimes not the best way to create a table, but if you really need to do that, you probably want to preallocate both the size and the data types of the table's variables, and then assign each row.

>> t = table(cell(4,1),NaN(4,1),NaN(4,1),NaN(4,1),'VariableNames',{'f1','f2','f3','f4'}) t = 4×4 table f1 f2 f3 f4 __ ___ ___ ___ [] NaN NaN NaN [] NaN NaN NaN [] NaN NaN NaN [] NaN NaN NaN >> t(1,:) = {'001' 2 3 4} t = 4×4 table f1 f2 f3 f4 _____ ___ ___ ___ '001' 2 3 4 [] NaN NaN NaN [] NaN NaN NaN [] NaN NaN NaN >> t(2,:) = {'002' 5 6 7} t = 4×4 table f1 f2 f3 f4 _____ ___ ___ ___ '001' 2 3 4 '002' 5 6 7 [] NaN NaN NaN [] NaN NaN NaNNote the right-hand side of those assignments. Normally, if the LHS is a parenthesis subscript expression, the RHS needs to be the same type, a table in this case. But table providea a conveneince where a RHS that's a cell array is treated as if cell2table were called on it. Because you have mixed types in each row, that's helpful.

The first time through your function, , so with the values in your input vector you end up assigning:

You can check that:

The next time through your loop you have , and then assign:

But this causes the error because your array expects the new string assignment, along the 3rd dimension of your array, to also have length = 6 (the length of 'medium'), since that is the value you used to initialize your array.

Your array sees this as an incompatible assignment, giving the error.

To get around this you will need to use something like a Cell array, which can accommodate variable data sizes and even types.

So your code will need to look something like this (note the change from () to {} for the array):

For example, executing this modified function on your data gives:

I don't know if this works correctly throughout the remainder of your application but at least this gives you the proper data structure.

Final comment, I don't understand why you have 3 dimensions in your array, why don't you just use:

Giving the output (arbitrary input vector):

## 0 Replies to “Structure Subscripted Assignment Dimension Mismatch In Matlab”