How can I write cell array data AND numeric matrix data in the same UITable in AppDesigner?

2 visualizzazioni (ultimi 30 giorni)
I am attempting the simple task of displaying data in a 8x7 UITable in an AppDesigner app. My intention is to have the first column contain data from an 8x1 cell array that contains labels:
S={S1;S2;S3;S4;S5;S6;S7;S8};
The remaining six columns will contain numeric data from various numeric matrices (this part is working properly).
I can't seem to sucessfully get the cell data and the matrix data to show on the same table. I get a horzcat error. This is my attempt:
app.UITable.Data=[S{:,1},D(2:end,1),Long,Lat,Elev,X2,Y2];
All of the matrices are 8 rows in length and the cell array is also 8 rows in length. Any help with this would be much appreciated. Thank you.

Risposta accettata

Umar
Umar il 25 Ott 2024

Hi @Jack Lebar ,

To successfully display both cell array data and numeric matrix data in a UITable in MATLAB's AppDesigner, it is essential to ensure that the data types are compatible for concatenation. In MATLAB, cell arrays and numeric arrays cannot be directly concatenated using standard horizontal concatenation. Instead, we need to convert the numeric data into a cell array format before concatenation. You have an 8x1 cell array S containing labels and several numeric matrices (e.g., D, Long, Lat, Elev, X2, Y2), all of which have 8 rows. To concatenate the cell array with the numeric matrices, convert the numeric matrices into cell arrays. This can be done using the num2cell function. Once all data is in cell array format, you can concatenate them horizontally. Finally, assign the concatenated data to the Data property of the UITable. Here is the complete code that you can use in your AppDesigner app:

% Assuming you have the following data:
S = {'S1'; 'S2'; 'S3'; 'S4'; 'S5'; 'S6'; 'S7'; 'S8'}; % 8x1 cell array
D = rand(8, 1); % Example numeric matrix (8x1)
Long = rand(8, 1); % Example numeric matrix (8x1)
Lat = rand(8, 1); % Example numeric matrix (8x1)
Elev = rand(8, 1); % Example numeric matrix (8x1)
X2 = rand(8, 1); % Example numeric matrix (8x1)
Y2 = rand(8, 1); % Example numeric matrix (8x1)
% Convert numeric matrices to cell arrays
D_cell = num2cell(D);
Long_cell = num2cell(Long);
Lat_cell = num2cell(Lat);
Elev_cell = num2cell(Elev);
X2_cell = num2cell(X2);
Y2_cell = num2cell(Y2);
% Concatenate all data into a single cell array
combinedData = [S, D_cell, Long_cell, Lat_cell, Elev_cell, X2_cell, Y2_cell];
% Assign the combined data to the UITable
app.UITable.Data = combinedData;
% Optionally, set the column names for better clarity
app.UITable.ColumnName = {'Labels', 'D', 'Long', 'Lat', 'Elev', 'X2', 'Y2'};

Please see attached.

In the above code snippet, cell array S and numeric matrices are defined. The numeric matrices are generated using rand for demonstration purposes. Each numeric matrix is converted to a cell array using num2cell, which allows for proper concatenation with the cell array S. The cell array S and the converted numeric cell arrays are concatenated horizontally into combinedData. The concatenated data is assigned to the Data property of the UITable. Additionally, column names are set for clarity.

By following the steps outlined above and utilizing the provided code, you should be able to successfully display both cell array and numeric data in your UITable without encountering any concatenation errors.

Hope this helps.

Please let me know if you have any further questions.

Più risposte (1)

dpb
dpb il 24 Ott 2024
VNAMES={'Labels','Distance?','Longitude','Latitude,','Elevation','X','Y'};
tData=table(S{:,1},D(2:end,1),Long,Lat,Elev,X2,Y2,'VariableNames',VNAMES)
app.UITable.Data=tData;
Use the builtin ability of the uifigure version of uitable to use the MATLAB table data class and make life simpler for yourself.

Prodotti


Release

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by