how to add string into cell array in matlab

To add a string to a cell array in MATLAB, you can use the curly braces {} to access and modify a specific cell of the array. Here's an example:

main.m
% Create an empty cell array
myCellArray = cell(3, 1);

% Add a string to the first cell
myCellArray{1} = 'Hello';

% Add a string to the second cell
myCellArray{2} = 'world';

% Display the cell array
disp(myCellArray);
221 chars
12 lines

This will output:

main.m
'Hello'
'world'
[]
19 chars
4 lines

Note that the third cell is empty because we initialized the cell array with 3 rows and 1 column using the cell function, but only filled the first two rows.

You can add more strings to the cell array by assigning them to a new index:

main.m
% Add another string to the cell array
myCellArray{3} = 'in MATLAB';

% Display the updated cell array
disp(myCellArray);
122 chars
6 lines

This will output:

main.m
'Hello'
'world'
'in MATLAB'
28 chars
4 lines

related categories

gistlibby LogSnag