Matlab GUI Excel Table
Mostrar comentarios más antiguos
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 comentarios
Mario Malic
el 11 de Mzo. de 2021
Editada: Mario Malic
el 11 de Mzo. de 2021
Replace this line and see how it goes.
nr = table(Parameter,Values);
I don't know where do you convert double to a function handle, so that error is suspicious.
How to edit existing data in Excel fields, if it's really necessary to do it from MATLAB, doing it in UITable is good enough.
Removed: app.t is a table, nr is a cell array. You can't concantencate them just like that.
Walter Roberson
el 11 de Mzo. de 2021
app.t is a table, nr is a cell array. You can't concantencate them just like that.
That turns out not to be the case. Using a cell array is the approved way to add rows to a table. See https://www.mathworks.com/help/matlab/matlab_prog/add-and-delete-table-rows.html?searchHighlight=add%20rows%20table&s_tid=srchtitle#AddAndDeleteTableRowsExample-3
Mario Malic
el 11 de Mzo. de 2021
Oh, I see. Thanks Walter.
Respuesta aceptada
Más respuestas (1)
Walter Roberson
el 11 de Mzo. de 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 comentarios
Demonstration of putting in handles:
fig = figure(1)
Notice that fig has Number property of 1
fignum = double(fig)
You can ask to convert the handle to double, and it will give you 1
ax = axes(fig);
h = plot(ax, rand(1,10));
t = table(fignum)
The one variable in the table is a double precision number
nr = {h} %data to be added to table
t = [t; nr] %success!
nr = {1}
t = [t; nr] %success! because the numeric value happens to match an existing handle
nr = {2}
t = [t; nr] %fail! because 2 does not happen to match an existing handle object
Obscure enough for you?
Walter Roberson
el 11 de Mzo. de 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
Categorías
Más información sobre Tables en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
