A variable index must be a real positive integer Issue in app designer
Show older comments
Hi,
I have created a callback on the Control Total button (please see image) to sum of the field of my table based on the drop down value. It successfully sums the field for numeric data, however when I run the application and want to sum company code, which is the first item in my drop down, it gives me the following error; A variable index must be a real positive integer.
I am able to sum company code if I change the drop down first and go back to it, but cannot sum the field without having to change the drop down value, otherwise the error shows.
Any way I can fix this?
10 Comments
Without seeing the full text of the error message and any code, all we can say is that you're indexing a variable and that your index is not positive or not an integer.
This has nothing to do with sum except if you have made the common beginner mistake of calling one of your variables sum.
Tamim Hussain
on 12 Oct 2018
Tamim Hussain
on 12 Oct 2018
Kevin Chng
on 12 Oct 2018
Is app.ddValue the value of your dropdown value? Normally, it will be app.dropdown.Value. By default, it will return you the option string instead of numeric variable.
Do you mind attach your script here? If it is in app designer, copy the whole script and paste in your .m script, then upload here.
Tamim Hussain
on 15 Oct 2018
Guillaume
on 15 Oct 2018
Tamin, follow the instructions in my answer. When the code errors and breaks into the debugger what is the exact value of app.ddValue?
Note that until the user selects something in your dropdown box, the value of app.ddValue will be []. Using [] as an index into the table will throw the error "A variable index must be a real positive integer."
And of course if app.ddValue is numeric but not a positive integer, you'll get the same error. If app.ddValue is a positive numeric integer greater than the number of variables you'll get the error "Variable index exceeds table dimensions" instead. If app.ddValue is a char array or string and not a valid variable name, you'll get "Unrecognized variable name" instead.
Kevin Chng
on 15 Oct 2018
Edited: Kevin Chng
on 15 Oct 2018
app.FieldsDropDown.Value will not return you the number of position of your selection. It will return you the name of option instead.
Try:
function FieldsDropDownValueChanged(app, event)
for i=1:1:length(app.FieldsDropDown.Items)
if strcmp(app.FieldsDropDown.Items(i), app.FieldsDropDown.Value)
k=i;
end
end
app.DropDownLabel.Text = num2str(k);
app.ddValue = k;
end
Therefore, compare the selection value and items of dropdown to get the position of your selection, then assign the numeric value to app.ddValue.
If it is working for you, then i will repost in the answer there for your accept.
Guillaume
on 15 Oct 2018
"app.FieldsDropDown.Value will not return you the number of position of your selection"
Indeed, it returns the content of the dropdown. That's not particularly a problem, as long as whatever that content is either a strictly positive integer less than or equal to the number of variables in the table or that it is a char array/string that is a valid variable name of the table.
That is for the line
total = sum(app.UITable.Data.(app.ddValue));
to work, the following values are ok:
app.ddValue = 1;
app.ddValue = width(app.UITable.Data);
app.ddValue = 'somevalidvariablename';
but not:
app.ddValue = []; %throws: A variable index must be a real positive integer
app.ddValue = -1; %throws: A variable index must be a real
app.ddValue = 1.5; %throws: A variable index must be a real
app.ddValue = width(app.UITable.Data) + 1; %throws: Variable index exceeds table dimensions
app.ddValue = 'invalid var name'; %throws: unrecognized variable name 'invalid var name'
Kevin Chng
on 15 Oct 2018
app.ddValue = []; %throws: A variable index must be a real positive integer
app.ddValue = -1; %throws: A variable index must be a real
app.ddValue = 1.5; %throws: A variable index must be a real
app.ddValue = width(app.UITable.Data) + 1; %throws: Variable index exceeds table dimensions
app.ddValue = 'invalid var name'; %throws: unrecognized variable name 'invalid var name'
Great to see this to explain why a variable index must be a real positive integer. +1
Accepted Answer
More Answers (0)
Categories
Find more on Develop Apps Using App Designer 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!