write a string into a excel cell in matlab

To write a string into an Excel cell in Matlab, you can use the xlswrite function. This function writes a numeric or a cell array to an Excel file. To write a string, you have to pass it as cell array. Here's an example:

main.m
fileName = 'example.xls';
sheetName = 'Sheet1';
stringToWrite = 'Hello, world!';
cellToWrite = {stringToWrite};
startCell = 'A1';

xlswrite(fileName, cellToWrite, sheetName, startCell);
186 chars
8 lines

In this example, the xlswrite function writes the cell array cellToWrite to an Excel file named example.xls, in the sheet named Sheet1, starting from the cell A1. The contents of the cell will be the string 'Hello, world!'. Note that we passed the string as a cell array {stringToWrite}.

You can also write multiple strings to different cells, by creating a larger cell array, where each element is a string:

main.m
fileName = 'example.xls';
sheetName = 'Sheet1';
stringsToWrite = {'Hello', 'world', '!', 'This', 'is', 'a', 'example.'};
cellsToWrite = reshape(stringsToWrite, 7, 1);
startCell = 'A1';

xlswrite(fileName, cellsToWrite, sheetName, startCell);
242 chars
8 lines

In this example, the xlswrite function writes a cell array with 7 elements to an Excel file named example.xls. The cell array is created by the reshape function, which takes a row vector of strings, and reshapes it to a column vector of cells. Finally, the cell array is written to the Excel file, starting from the cell A1.

gistlibby LogSnag