How do I concatenate a cell array of numeric and character data as a string within MATLAB?
3 views (last 30 days)
Show older comments
How do I concatenate a cell array of numeric and character data as a string within MATLAB?
I want to display the contents of my 5-by-5 cell array "TEST" onto a listbox in my GUI so that it is displayed as 5 lines, each representing a row of the cell array. However, when I use the command
set(hObject,'String',test)
the listbox displays all 25 elements separately in 25 lines in the listbox.
Accepted Answer
MathWorks Support Team
on 27 Jun 2009
The following example will manipulate your cell array to be properly displayed in any MATLAB environment:
% Prepare sample cell array, test (3x3)
test={'row 1',1,2;'row 2',2,4;'row 3',3,6};
% Ensure that all elements of cell array are strings for display
for i=1:numel(test) % Goes through all elements
if ~iscellstr(test(i))
test{i}=num2str(test{i});
end
end
% Insert spaces to separate the strings for display purposes
test=strcat(test,{' '});
% Concatenate the cell array of strings
test=cell2mat(test)
% You can specify the first row with the following
test(1,:)
% Likewise for the second and third rows
% This example will work in GUI callbacks.
0 Comments
More Answers (0)
See Also
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!