from JTable to structure fieldname
조회 수: 4 (최근 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!