Matlab GUI Excel Table

5 views (last 30 days)
I
I on 11 Mar 2021
Commented: I on 14 Mar 2021
properties (Access = private)
t % Table to share between callback functions
end
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: ExcelDataButton
function ExcelDataButtonPushed(app, event)
app.t = readtable("Book1.xlsx","Sheet",1);
app.UITable.Data = app.t;
%t.Properties.VariableNames{1} = 'Parameter';
%t.Properties.VariableNames{2} = 'Values';
%app.UITable.ColumnName = t.Properties.VariableNames;
end
% Button pushed function: AddButton
function AddButtonPushed(app, event)
Parameter = app.ParameterEditField.Value;
Values = app.ValuesEditField;
nr = {Parameter Values};
app.UITable.Data = [app.t;nr]; //% Line with error is here
end
end
I get an error on the "Line with error is here" that reads:
An error occurred when concatenating the table variable 'Values' using VERTCAT.
Caused by:
Error using matlab.ui.control.NumericEditField/vertcat
Cannot convert double value 1 to a handle
I'm not sure how to solve this yet.
Additionally, I want to find a good way to edit existing data in excel fields.
  3 Comments
Walter Roberson
Walter Roberson on 11 Mar 2021
app.t is a table, nr is a cell array. You can't concantencate them just like that.
Mario Malic
Mario Malic on 11 Mar 2021
Oh, I see. Thanks Walter.

Sign in to comment.

Accepted Answer

I
I on 11 Mar 2021
I'm not quite sure how statename works, but I haven't tried this yet, but I think I need to do this:
statename = "UserData"; %Set statename = to the current values?
statevalue = ValuesEditField; %Then set the statevalue equal to my edit field? (so I need to add a callback function for this edit field?)
table3 = table(statename, statevalue) %
I think:
My user data are the column names
table3 is a new variable name
statevalue is equal to the edit field
and I'm not sure what statevalue represents,
I tried again with and without adding a callback function for the edit field related to adding data to the excel table.
I'm really not sure what I'm doing wrong yet.
  3 Comments
Walter Roberson
Walter Roberson on 11 Mar 2021
The important point is that you were passing in a character vector, and you need to Not Do That. Pass in a string() object or a cell array of character vectors instead.
I
I on 14 Mar 2021
I fixed it and I just sort of understand how, but at least I can move onto the next problem.

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 11 Mar 2021
app.t = readtable("Book1.xlsx","Sheet",1);
That is a table object
Values = app.ValuesEditField;
That is the handle of a uieditfield, not the values stored inside the field.
nr = {Parameter Values};
You put the handle in a cell array (not an error in itself)
app.UITable.Data = [app.t;nr];
You try to vertcat the cell array to the end of the table. vertcat of a cell array with a table object is defined to add additional rows, so the kind of operation you are using is generally permitted. However, the values being added through the cell array must be compatible types with the values already in the table. In particular, the second column you are adding through the cell is a handle object, which requires that any existing value in the column also be a handle or be convertable to a handle.
The existing value in the column includes at least one value that is double precision 1, and MATLAB is failing to convert that to a handle.
It is sometimes possible to convert double precision numbers to a handle... provided that there happens to be an object with that value as a numeric handle. I will demonstrate in a comment. However, you do not happen to have an existing object with numeric handle 1, so the conversion of 1 to handle fails.
The root problem is that you should be pulling the value out of ValuesEditField instead of copying the handle to the field.
I would suspect that you also want to store the updated table into app.t to be retrieved next time so that more can be added to it.
  3 Comments
I
I on 11 Mar 2021
Edited: I on 11 Mar 2021
I attempted to add a parameter to my table again, and the below is a different error.
Error using table (line 260)
Invalid parameter name: Cd.
Caused by:
You might have intended to create a one-row table with the character vector 'wing' as one of its variables. To store text data in a table, use a string array or a cell array of character vectors rather than character arrays. Alternatively, create a cell array with one row, and convert that to a table using CELL2TABLE.
I don't get this explanation. I intend to make two columns to my understanding. Is there a fix for this?
Walter Roberson
Walter Roberson on 11 Mar 2021
Somewhere in the code, you have a call of the form
name = table(expression, more stuff)
where expression is a character vector that has the content 'Cd' .
MATLAB cannot (easily) distinguish between the case where you pass in a variable or expression that happens to be a character vector, versus the case where you intended the content of the expression to be part of a name/value pair. For example,
table1 = table('UserData', 3) %option!
statename = 'UserData';
statevalue = 3;
table2 = table(statename, statevalue) %option??
for both of those table() calls, what table() receives is the parameter list 'UserData', 3 . table does not get told that in one case variables were passed in that just happened to contain that content, whereas in the other case that the user used constants specifically to "intend" to use the table option 'UserData' with value 3.
Because of this, when MATLAB sees a character vector passed into a position that might be either data intended to store into the table, or possibly intended as a name/value option pair, MATLAB issues an error message instead of fishing through the list of permitted options to see if it was a match or not.
In order to get your content Cd into the table, you have a couple of methods available:
statename = "UserData"; %string instead of character vector
statevalue = 3;
table3 = table(statename, statevalue) %table variable named statename, table variable named statevalue
statename = {'UserData'}; %cell array of character vectors
statevalue = 3;
table4 = table(statename, statevalue) %table variable named statename, table variable named statevalue

Sign in to comment.

Categories

Find more on Desktop in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!