MATLAB: Searching for a specific numbers inside a text file

MATLABStatistics and Machine Learning Toolboxtext filetext;textscan

Hello,
I have two questions :
1- I have a text file named sample1.txt , I want this to be formatted to sample1_formatted.txt. Note that, each line is separated by a comma.
defaultdict(<class 'list'>, {'cube28_cluster1': [46, 68], 'cube28_cluster0': [86],'cube29_cluster1': [46], 'cube29_cluster0': [82, 86], 'cube33_cluster1': [8], …etc becomes
defaultdict(<class 'list'>,
{'cube28_cluster1': [46, 68],
'cube28_cluster0': [86],
'cube29_cluster1': [46],
'cube29_cluster0': [82, 86],
'cube33_cluster1': [8],
2- In sample1_formatted.txt file, I want to search for numbers in the matrix-like places , e.g. if I wanted 46, then the result output is
{'cube28_cluster1': [46, 68],
'cube29_cluster1': [46],
Thank you in advance.

Best Answer

S = fileread('sample1.txt');
newS = regexprep(S, '],', '],\n');
fid = fopen('sample1_formatted.txt', 'w');
fwrite(fid, newS);
fclose(fid)