Error using table: all variables must have the same number of rows

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

>> 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.
My plan was originally have the values be calculated and then input into a table. However, I ran into difficulties figuring out how to do this and tried to eliminate some variables to simplify things.
Ideally the value Monthly Salary would be multiplied by the table Tax Rates to yield another table, Withholding Rates. The table for Withholding Rates would be summed to yield the value for Withholding. These values would be passed to the functions creating the Payroll Payments and Payroll Events tables. Withholding Total and Disbursment Total are supposed to summarize the values for the Disbursment and Withholding Columns when there are multiple entries.
The result is supposed to be a table that can be updated. Upon second thought, my issue seems to be making functions that reference each other.
Attempting to rectify this issue has resulted in me running into the error "undefined function 'mtimes' for input arguments of type 'cell' due to attempting to sum a function that points to a table.
fixing functions leads to error with table variables requiring same number of rows again
Properties now looks like this:
% Payroll
MonthlySalary = {12500};
Tax_Rates = {0.015,0.0505,0.062,0.0145};
DatePaid = {};
Employee = {};
WD = {};
Disbursment = {};
Disbursment_Total = {};
Withholding = {};
Withholding_Federal = {};
Withholding_State = {};
Withholding_SocialSecurity = {};
Withholding_Medicare = {};
Withholding_Total = {};
Bounce = {};
functions like this:
function WithholdingRates = WithholdingRates(app)
WithholdingRates = table(mtimes(app.MonthlySalary,app.Tax_Rates));
app.Withholding = sum(WithholdingRates);
app.Withholding_Federal = WithholdingRates(1,1);
app.Withholding_State = WithholdingRates(1,2);
app.Withholding_SocialSecurity = WithholdingRates(1,3);
app.Withholding_Medicare = WithholdingRates(1,4);
app.Disbursment = app.MonthlySalary - app.Withholding;
app.WD = table(app.Withholding,app.Disbursment);
app.Withholding_Total = sum(app.WD(:,1));
app.Disbursment_Total = sum(app.WD(:,2));
end
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
function ListEmployeesButtonPushed(app, event)
app.Tab.Title = 'Employees';
app.Tab2.Title = 'N/A';
app.UITable.Data = Employees(app);
app.UITable.ColumnName = {'Last Name','First Name','Address Line 1','Address Line 2','City','State','Zip Code','Social Security #','# of Withholdings','Yearly Salary'};
end
Fixed a bunch, but still end up with same problem of tables having variables with different numbers of rows
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.
Figured out I had to have the property referenced in the function aready formatted in an array, rather than having the function construct a table out of multiple properties. However, this means that it is perceived as one variable, so only one Column Name will be labled despite there being multiple columns in the table! Any suggesions on how to fix this?
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>

Sign in to comment.

Answers (1)

properties (Access = public)
% Employees
% EmployeeVars = {'Last Name' 'First Name' 'Address Line 1' 'Address Line 2' 'City' 'State' 'Zip Code' 'Social Security #' '# of Withholdings' 'Yearly Salary'};
Employees = {'Doe','John','Address 1','Address 2','Waltham','MA','02452','280-663-9941',0,'150000.00'};
NewEmployees = {};
% EmployeeLastName = {'Doe'};
% EmployeeFirstName = {'John'};
% EmployeeAddressLine1 = {'Address 1'};
% EmployeeAddressLine2 = {'Address 2'};
% EmployeeCity = {'Waltham'};
% EmployeeState = {'MA'};
% EmployeeZip = {'02452'};
% SocialSecurityNumber = {'280-663-9941'};
% Number_of_Withholdings = {0};
% YearlySalary = {'15000'};
EmployeeList = {};
% NewEmployee ={};
function Employees = EmployeeTable(app)
% Employees = table(app.EmployeeLastName,app.EmployeeFirstName,app.EmployeeAddressLine1,app.EmployeeAddressLine2,app.EmployeeCity,app.EmployeeState,app.EmployeeZip,app.SocialSecurityNumber,app.Number_of_Withholdings,app.YearlySalary);
Employees = table(app.Employees);
% writetable(Employees,'Employees.csv');
end
function ListEmployeesButtonPushed(app, event)
app.Tab.Title = 'Employees';
app.Tab2.Title = 'N/A';
app.UITable.Data = EmployeeTable(app);
% app.UITable.ColumnName = {'Last Name' 'First Name' 'Address Line 1' 'Address Line 2' 'City' 'State' 'Zip Code' 'Social Security #' '# of Withholdings' 'Yearly Salary'};
The problem is that I need the ability to append the array and have the table update to show the changes. If I have the array being assembled from variables by a function, 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.

3 Comments

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.
figured it out
app.UITable.Data = table(app.EmployeeLastName,app.EmployeeFirstName,app.EmployeeAddressLine1,app.EmployeeAddressLine2,app.EmployeeCity,app.EmployeeState,app.EmployeeZip,app.SocialSecurityNumber,app.Number_of_Withholdings,app.YearlySalary);
I've never used the UITable -- I presume the above syntax replaces the whole .Data property?
Is there a way to reference a row?

Sign in to comment.

Categories

Find more on Develop Apps Using App Designer in Help Center and File Exchange

Products

Release

R2020a

Asked:

on 4 Jul 2020

Commented:

dpb
on 8 Jul 2020

Community Treasure Hunt

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

Start Hunting!