from JTable to structure fieldname
3 次查看(过去 30 天)
显示 更早的评论
hello, I am developing a GUI in Matlab using Java, I added a table (1 column, user-defined number of rows) I want to insert names in the rows and use those names as fieldnames to define structures but the variable coming from the table is a cell array with vectors containing the string between square breakets and I can't use them for fieldnames (I guess because of the breakets, it says 'invalid fieldname' or something), I realize the problem can be cause by the getValue istance that is referred to vectors, not strings, but I don't know what to do otherwise. Thank you
2 个评论
Guillaume
2018-7-11
It would be useful if you could show us the code you use to extract the data from the table.
采纳的回答
Guillaume
2018-7-11
A word of caution first: five minutes ago, I didn't know anything about JTable.
JTable.getModel.getDataVector returns a vector of vectors. The outer vector is the rows and the inner vector is the columns. So, your data.satellitetable.getModel.getDataVector.elementAt(i-1) is the vector of columns of the ith row. To actually get the content of the colum you still need to iterate over that vector, or since your table only has one column, just access that column
rowvectors = data.satellitetable.getModel.getDataVector; %this is a Vector of Vectors
for row = 0:rowvectors.size-1 %iterate over the rows
columnvector = rowvectors.elementAt(row); %columns of the current row
value1stcolumn = columnvector.elementAt(0); %value in 1st column
%...
|value1stcolumn | will be a char array with the exact content of the cell.
Alternatively, you can use the ToArray method to convert the column vector to a Java Array, that you can then convert into a cell array of whatever is in these columns (most likely char arrays), so:
rowvectors = data.satellitetable.getModel.getDataVector; %this is a Vector of Vectors
for row = 0:rowvectors.size-1 %iterate over the rows
columnvector = rowvectors.elementAt(row); %columns of the current row
columnvalues = cell(columnvector.ToArray); %convert vector to array, then to matlab cell array
value1stcolumn = columnvalues{1};
%...
datamodel = data.satellitetable.getModel;
for row = 0:datamodel.getRowCount-1
valueat1stcolumn = datamodel.getValueAt(row, 0);
%....
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Migrate GUIDE Apps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!