Assignment has more non-singleton rhs dimensions than non-singleton subscripts

7 Ansichten (letzte 30 Tage)
I've seen this error asked about a few times, but not when a individual value is being assigned to the matrix index. Since I'm not trying to assign an entire matrix to the index in "hands", what could be causing this then?
table = get(handles.deckList,'string');
hands = zeros(2,7);
%parse table cards into both hands
for i = 1:2
for j = 1:7
if i == 1 && j == 1
hands(i,j) = table{1}; %Error occurs here
hands(i,j+1) = table{2};
elseif i == 1 && j > 2
hands(i,j) = table{j+2};
elseif i == 2 && j == 1
hands(i,j) = table{3};
elseif i == 2 && j >= 2
hands(i,j) = table{j+2};
end
end
end
This is the code I'm working with. I've also tried storing the value from the cell array to another variable and assigning that but it throws another error about matrix dimensions which is another problem.That code is below.
card = table{1};
hands(1) = card;
card = table{2};
hands(3) = card;
card = table{5};
hands(5) = card;
card1 = table{6};
hands(7) = card;
%and so on for the rest of the matrix.

Akzeptierte Antwort

Adam
Adam am 24 Nov. 2016
Bearbeitet: Adam am 24 Nov. 2016
What is handles.deckList?
I am guessing it is some kind of list control so that its string is a cell array of strings so
table{1}
is itself a string (or char array). You can't assign a string to a scalar element of a numeric array.
If these strings are all numeric then maybe you intend to use
hands(i,j) = str2double( table{1} )
but that is just a guess. It is hard to say without knowing exactly what the string is that is in handles.deckList.
This simple code produces the same error and is what I am guessing your code is doing:
>> hands = zeros(2,7);
>> hands(2,3) = 'stuff'
Assignment has more non-singleton rhs dimensions than non-singleton subscripts
  1 Kommentar
Romaine Rattray
Romaine Rattray am 24 Nov. 2016
The string in handles.deckList was a cell array of number strings representing the cards on the board. eg. table{1} would be '15', table{2} = '2' and so on. My downfall was not understanding the type of matrix I created "hands" as.
Using your suggestion had cleared up the error, thanks for the help.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Walter Roberson
Walter Roberson am 24 Nov. 2016
There are two kinds of strings in MATLAB. The older kind is the one you are using, and it is really a row vector of characters. Unless that happens to be exactly one character it is not going to fit in a single location in the numeric vector "hands"
Perhaps you want to use str2double to convert the string into a numeric value?

Kategorien

Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by