Matlab GUI Excel Table
조회 수: 4 (최근 30일)
이전 댓글 표시
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
Walter Roberson
2021년 3월 11일
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
채택된 답변
I
2021년 3월 11일
댓글 수: 3
Walter Roberson
2021년 3월 11일
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.
추가 답변 (1개)
Walter Roberson
2021년 3월 11일
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
Walter Roberson
2021년 3월 11일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Tables에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!