I generated a table, and based on it I wish to save every row in the Info column in a separate file. Each file name is based on the corresponding value of the Code column.
The table is as follows:
Code Info
'S1' '38 11;12 11;21 11'
'J1' '43 11;61 71'
'L1' '38 11;18 19'
'D1' '40 11;15 41'
'B1' '49 11;21 22;1 22;3 22;4 22'
Is there a better and more elegant way to do it which does not involve cell/table/string conversion?
How can I save every Info row data in multiply rows where the ;
acts like a new line?
For example: B1.txt content should be:
49 11
21 22
1 22
3 22
4 22
Instead of
49 11;21 22;1 22;3 22;4 22
Code:
Code = {'S1';'J1';'L1';'D1';'B1'};
Info = {'38 11;12 11;21 11';'43 11;61 71';'38 11;18 19';'40 11;15 41';'49 11;21 22;1 22;3 22;4 22'};
T = table(Code,Info);
for i = 1:height(T)
fileNameCode = string(table2cell(T(i,1)));
fileContent = (table2cell(T(i,2)))
filename= strcat("E:/",fileNameCode,".txt")
writecell(fileContent,filename)
end
The solution is relatively simple, even without using cell/table/string.
Code
cell array: for i = 1:length(Code)
fileNameCode = Code{i}
.Info{i}
.;
with new line character \n
: fileContent = strrep(Info{i}, ';', '\n');
.[fileNameCode, '.txt']
.fullfile
for building the full file path.fopen
to open the file as a text file f = fopen(filename, 'wt');
.fileContent
using fprintf
: fprintf(f, [fileContent, '\n']);
.'\n'
at the end is optional).fclose(f);
.Code sample:
Code = {'S1';'J1';'L1';'D1';'B1'};
Info = {'38 11;12 11;21 11';'43 11;61 71';'38 11;18 19';'40 11;15 41';'49 11;21 22;1 22;3 22;4 22'};
for i = 1:length(Code)
fileNameCode = Code{i};
fileContent = strrep(Info{i}, ';', '\n'); % Replace all ";" with new line character.
filename = fullfile('E:', [fileNameCode, '.txt']);
f = fopen(filename, 'wt'); % Open a text file for writing.
fprintf(f, [fileContent, '\n']); % Write the data - add new line character at the end.
fclose(f); % Close the file
end
Content of D1.txt
for example:
40 11
15 41
We may use the Table fields directly, up until the point when we have to write to the files.
Since we are writing each row to separate file, we have to iterate the rows.
strrep
may be applied to multiple rows:
T.Info = strrep(T.Info, ';', new_line); % Replace all ";" with new line character (characters).
We may use rowfun for creating the file names without a loop.
The code uses an anonymous lambda function, and the syntax is complicated:
T2 = rowfun(@(x) {fullfile('E:', [x, '.txt'])}, T, 'InputVariables', 'Code', 'OutputVariableName', 'filename', 'ExtractCellContents', true);
The code sample uses dlmwrite
instead of fopen
, fprintf
and fclose
(for showing a different approach).
Updated code sample:
Code = {'S1';'J1';'L1';'D1';'B1'};
Info = {'38 11;12 11;21 11';'43 11;61 71';'38 11;18 19';'40 11;15 41';'49 11;21 22;1 22;3 22;4 22'};
T = table(Code, Info);
if isunix
new_line = char(13); % New line in Unix is one character: CR
else
% When using dlmwrite, we have to define the new line as CR LF (in Windows).
new_line = char([13, 10]); % New line in Windows is two characters: CR LF
end
T.Info = strrep(T.Info, ';', new_line); % Replace all ";" with new line character (characters).
% Apply the function fullfile('E:', [x, '.txt']) to each row of the table (apply it only for Code column).
% Place each file name in a cell array - required in case the names have different lengths.
% Note: We don't have to use rowfun, we may create the file name inside the loop.
T2 = rowfun(@(x) {fullfile('E:', [x, '.txt'])}, T, 'InputVariables', 'Code', 'OutputVariableName', 'filename', 'ExtractCellContents', true);
for i = 1:height(T)
filename = cell2mat(T2.filename(i)); % Convert cell array to character array.
fileContent = cell2mat(T.Info(i)); % Convert cell array to character array.
dlmwrite(filename, fileContent, 'delimiter', '') % Use dlmwrite (instead of fopen, fprintf and fclose).
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With