I want to be able to run a loop through my current code. I have a cell array with 423 cells. Each cell is a long string of smaller strings that I am trying to separate by the delimiter (;) and I want it to loop through each cell, split the string according to the delimiter and write the cell to the excel sheet i have created. But it only does the most recent cell rather than aggregating all of the cells.
I have an excel sheet (see screenshot), in column C I have lots of small sentences that are all separated with the delimiter ';'. I want the code to go through each row in excel and split up this big bunch of text so that each sentence between the delimiters has its own cell rather than being all together. I have managed to make the code work for one row, so the code seperates all of the small strings in to its own new row or new cell. But I cannot do this for every single row in excel, the code simply overwrites each one. I want to create a loop and iterate the process. I then want it to write this big list to a new excel sheet. I have tried to now create an iteration loop but it is not working correctly; it writes the strings to excel but it is not capturing all of them for some reason.
Here is the code as plaintext:
clc
clear
close all
T = readtable("********","range", "C1:C424");
C = table2cell(T);
for i=2:423
splitlist = unique(strsplit(C{i:i},';'))
writecell(splitlist','not_list.xlsx')
end
You need to add WriteMode', 'append'
as input to writecell
.
I have only tested this online, but it appears to work fine.
for i = 1:numel(C)
split_str = unique(strsplit(C{i},';'))
writecell(split_str, 'not_list.xlsx', 'WriteMode','append');
end