from JTable to structure fieldname
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Francesco Fortunato
el 11 de Jul. de 2018
Comentada: Francesco Fortunato
el 11 de Jul. de 2018
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 comentarios
Guillaume
el 11 de Jul. de 2018
It would be useful if you could show us the code you use to extract the data from the table.
Respuesta aceptada
Guillaume
el 11 de Jul. de 2018
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);
%....
Más respuestas (0)
Ver también
Categorías
Más información sobre Migrate GUIDE Apps en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!