from JTable to structure fieldname

3 ビュー (過去 30 日間)
Francesco Fortunato
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 Center および File ExchangeMigrate GUIDE Apps についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by