from JTable to structure fieldname

조회 수: 4 (최근 30일)
Francesco Fortunato
Francesco Fortunato 2018년 7월 11일
댓글: Francesco Fortunato 2018년 7월 11일
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
Guillaume 2018년 7월 11일
It would be useful if you could show us the code you use to extract the data from the table.
Francesco Fortunato
Francesco Fortunato 2018년 7월 11일
if true
% code
end
modelsc = javax.swing.table.DefaultTableModel(1,1);
data.satellitetable = JTable(modelsc);
data.satellitetable.getColumnModel().getColumn(0).setHeaderValue('Spacecraft Name');
data.satellitetable.getTableHeader.setReorderingAllowed(0);
tableviewsc = JScrollPane(data.satellitetable);
tableviewsc.setPreferredSize(java.awt.Dimension(200,100));
c.insets = Insets(0,10,0,0);
c.anchor = GridBagConstraints.CENTER;
c.gridwidth = 1;
c.gridheight = 1;
c.gridx = 0;
c.gridy = 0;
gridbag.setConstraints(tableviewsc, c);
constellationpanel.add(tableviewsc,c);
and to read the values
if true
% code
end
input.Constellation.satnumber=data.satellitetable.getModel.getRowCount;
for i = 1:data.satellitetable.getModel.getRowCount
satellitear = data.satellitetable.getModel.getDataVector.elementAt(i-1);
if ischar(satellitear)
input.Constellation.SCnames(i) = satellitear;
else
if isempty(str2num(satellitear))
input.Constellation.SCnames(end+1) = satellitear;
else
input.Constellation.SCnames(end+1) = satellitear;
end
end
end

댓글을 달려면 로그인하십시오.

채택된 답변

Guillaume
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};
%...
But I believe simpler is to use the GetValueAt method of the DataModel:
datamodel = data.satellitetable.getModel;
for row = 0:datamodel.getRowCount-1
valueat1stcolumn = datamodel.getValueAt(row, 0);
%....
  댓글 수: 1
Francesco Fortunato
Francesco Fortunato 2018년 7월 11일
thank you so much! I am actually a beginner in all of this, as you surely understood, but I solve my problem from your answer

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by