Error using table: all variables must have the same number of rows
Show older comments
Trying to make an app that does some business and payroll functions, relevant parts are as follows:
Properties
% Payroll
MonthlySalary = {12500};
Tax_Rates = {0.015,0.0505,0.062,0.0145};
Withholding_Rates = {187.5,631.25,775,181.25};
Withholding_Federal = {187.5};
Withholding_State = {631.25};
Withholding_SocialSecurity = {775};
Withholding_Medicare = {181.25};
DatePaid = {};
Employee = {};
Disbursment = {};
Disbursment_Total = {};
Withholding = {};
Withholding_Total = {};
Bounce = {};
functions
function PayrollPayments = PayrollPayments(app)
PayrollPayments = table(app.DatePaid, ...
app.Employee, ...
app.Disbursment, ...
app.Withholding, ...
app.Disbursment_Total, ...
app.Withholding_Total);
end
function PayrollEvents = PayrollEvents(app)
PayrollEvents = table(app.MonthlySalary, ...
app.Bounce, ...
app.Withholding_Federal, ...
app.Withholding_State, ...
app.Withholding_SocialSecurity, ...
app.Withholding_Medicare, ...
app.Disbursment);
end
% Button pushed function: PayrollButton
function PayrollButtonPushed(app, event)
app.Tab.Title = 'Payroll Payments';
app.UITable.Data = PayrollPayments(app);
app.UITable.ColumnName = {'DatePaid','Employee','Disbursment','Withholding','Total Payroll Expenses','Total Withholding'};
app.Tab2.Title = 'Payroll Events';
app.UITable2.Data = PayrollEvents(app);
app.UITable2.ColumnName = {'Monthly Salary','Bounce','Federal Tax','State Tax','Social Security Tax','Medicare Tax','Amount Paid'};
Some investigation indicates that I may be causing the issue by mixing column and row variables when creating the table using payroll events. Could it be because I am declaring the variables as individual properties? If I leave out the variables, the table gets created without problem based on the column names.
I am not terribly great at programming, and this is my first time using the app builder - so I am quite stumped. I wanted to solve this problem before moving on to my issues with using tables that reference calculations performed using functions.
8 Comments
dpb
on 4 Jul 2020
>> whos Withhold*
Name Size Bytes Class Attributes
Withholding 0x0 0 cell
Withholding_Federal 1x1 120 cell
Withholding_Medicare 1x1 120 cell
Withholding_Rates 1x4 480 cell
Withholding_SocialSecurity 1x1 120 cell
Withholding_State 1x1 120 cell
Withholding_Total 0x0 0 cell
>>
You've got empty variables as well as those with one row and (one of those an array) -- a table has to have an entry for every column for every row so there has to be something for 'Withholding' and 'Withholding_Total' to begin with.
It's not totally clear to me what your idea is here; it would seem to creat the empty table and then when have a complete record it could be updated at that point.
Eli Blumen
on 4 Jul 2020
Eli Blumen
on 4 Jul 2020
Eli Blumen
on 4 Jul 2020
Eli Blumen
on 4 Jul 2020
dpb
on 4 Jul 2020
Well, you know what the problem is, you'll just have to make your design/implementation only try to create a table (or add a row to an existing one) when you have one or more complete records.
Eli Blumen
on 7 Jul 2020
dpb
on 7 Jul 2020
Again, show code -- you can certainly add to an existing table by row for each variable; in fact, that's the only way you can --there has to be a 1:1 correspondence to existing variables.
Just finished answering the "how" of that here -- mayhaps this will help you, as well <Answers/560489-adding-additional-data-to-previous-table>
Answers (1)
Eli Blumen
on 8 Jul 2020
3 Comments
dpb
on 8 Jul 2020
Use comments for follow-up instead of Answer...
" I am unsure how to either append the variables such that they form columns in the array, or append the array (or its components) when it is controlled by a function. Ideally the function would assemble the array and pass it off to another variable."
As in the example code I linked to above, you can create an array from whatever elements you need with the [] operator; if it is disparate data types as shown there then it will needs must be a cell array to hold the various pieces.
A function can return anything, arrays, cell arrays, tables, whatever...there's no difference in callback function and any other function in calling anything you want/need other than having the variables needed in scope.
Eli Blumen
on 8 Jul 2020
dpb
on 8 Jul 2020
I've never used the UITable -- I presume the above syntax replaces the whole .Data property?
Is there a way to reference a row?
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!