A variable index must be a real positive integer Issue in app designer

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.
Sorry, here is the code for calculating the sum for the column in the app table.
function ControlTotalButtonPushed(app, event)
total = sum(app.UITable.Data.(app.ddValue));
app.EditFieldTotal.Value = total;
if total == 0
app.Label.Text = 'Balance';
else
app.Label.Text = 'Unbalance';
end
The following error occurs: Error using app2/ControlTotalButtonPushed (line 68) A variable index must be a real positive integer.
That error message should also give the content of the line throwing the error (I did write full text in bold!). Presuming that line 68 is the total = ... line, have you looked at the content of app.ddValue when the error occurs?
Hi, yes the error occurs on line 68. App.ddvalue is the selected value from the fields drop down column. Could it be that the fields drop down values are empty before the data is imported? I should have actually mentioned the fields drop down items populate themselves based on csv file column names.
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.
Hi Kevin, I have attached the script.
Guillaume, the value of app.ddvalue is company code when I debug the file, even so, why is it showing an error although that field contains numeric data?
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.
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.
"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'
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

Sign in to comment.

 Accepted Answer

Could it be that the fields drop down values are empty before the data is imported?
It's simple to check using the debugger. At the command line, issue a
dbstop if error
before running your code. Run your code and when it breaks into the debugger because of the error, check the value of app.ddvalue.

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!