Unable to perform assignment with 0 elements on the right-hand side.
Show older comments
I am having some error with obtain a variable that contain a value from the table with the conditions that th AccelSNEditField input equals on of the values in the first column and to use that row and look at the 2nd column of the table to obtain the value for that variable:
BIA0=mergetables{app.AccelSNEditField == mergetables{:,1},2};
However, I am obtaining this error:
Unable to perform assignment with 0 elements on the right-hand side.
Any suggestions or resolutions?
Answers (2)
The actual problem is that you think that one (or more) of these values match:
app.AccelSNEditField == mergetables{:,1}
In fact they don't. Zero matches. None. Zilch. Nada.
And then you construct a comma-separated list with zero arrays in it and try to allocate those zero arrays to one array.
Does not compute. Not possible. No can do. Il n'est pas possible.
Solution:
That depends on what the problem is. I can see two main possibilities:
- Are matches known to exist? Then perhaps your data is not correct (e.g. missing data, wrong units, etc.), or the matching needs to take into account floating point error (e.g. compare absolute difference against a toleance), or throw an error as this indicates an obvious bug in the input data.
- Perhaps for some values no matches actually exist, in which case you need to add special-case handling to your code.
Image Analyst
on 20 Jul 2020
app.AccelSNEditField == mergetables{:,1}
is going to product a vector of true or false values, which evaluate to 1 or 0 when used as a numerical index. Evidently some of your comparisons are false, which evaluate to zero. There is no zero index for arrays. You can have the first row, but you cannot have the zeroeth row. In this matrix
m = [1, 2;
3, 4];
The first row is [1, 2], but what is the zeroeth row? There is no zeroeth row. See the FAQ for a thorough discussion:
8 Comments
"Evidently some of your comparisons are false, which evaluate to zero."
Logical indexing is not converted to numeric linear/subscript indexing, false is a perfectly valid array index**:
>> A = [2,3,5];
>> A(true)
ans =
2
>> A(false)
ans =
[]
This is just a basic example of a comma-separated list returning zero elements:
which is also exactly what the error message states. There is no error in the indexing, using one single false value is perfectly valid and permitted logical indexing, as has been discussed before:
** This example also makes it clear that it is perfectly valid to have logical indexing of a different size than that of the array being indexed into, with the only restriction that any true values must be within the array size.
Learn more about logical indexing:
Alexandra Philip
on 20 Jul 2020
"How can I ensure I do not have a zeroeth row?"
You don't. No MATLAB array has a zeroth row, all arrays start from row one.
"The input I put for the app.AccelSNEditField is equal to a value within the first column of mergetables array"
No, in fact that error makes it clear that none of the values match, which is why the index is all false.
That is the actual cause of the error. There is no "zero row" involved anywhere in this problem.
Image Analyst
on 20 Jul 2020
Edited: Image Analyst
on 20 Jul 2020
Thanks for the correction, Stephen (envision me slapping my forehead 😧).
Put this in your script just before the error
app.AccelSNEditField
mergetables{:,1}
Don't use semicolons. What do you see? Paste it back here. Also tell us what you want to do in the event that there are no matches. Anything or nothing? Just skip that line???
Alexandra Philip
on 20 Jul 2020
Image Analyst
on 20 Jul 2020
OK, then what does this say
whos mergetables
Evidently it's not a table or cell array, so let's see what it is. If it's not, then you can't use braces.
And what does it say about that variable in the workspace panel?
Alexandra Philip
on 20 Jul 2020
Image Analyst
on 21 Jul 2020
Well we're back to having no matches in the comparison:
app.AccelSNEditField == mergetables{:,1}
when you do this:
BIA0=mergetables{app.AccelSNEditField == mergetables{:,1},2};
It's hard to help when we don't have all the variables. Can you attach them in a .mat file:
AccelSNEditField = app.AccelSNEditField;
save('answers.mat', 'AccelSNEditField', 'mergetables');
Then attach answers.mat with the paper clip icon.
Categories
Find more on Logical 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!