我生成了一个表,并基于它我希望将信息列中的每一行保存在单独的文件中。每个文件名都基于代码列的相应值。
表格如下:
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'
Run Code Online (Sandbox Code Playgroud)
有没有更好、更优雅的方法来做到这一点,不涉及单元格/表/字符串转换?
如何将每个信息行数据保存在多个行中,其;行为就像一个新行?
例如:B1.txt内容应为:
49 11
21 22
1 22
3 22
4 22
Run Code Online (Sandbox Code Playgroud)
代替
49 11;21 22;1 22;3 22;4 22
Run Code Online (Sandbox Code Playgroud)
代码:
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
Run Code Online (Sandbox Code Playgroud)
解决方案相对简单,即使不使用单元格/表格/字符串。
Code胞数组:for i = 1:length(Code)fileNameCode = Code{i}。Info{i}。;为换行符\n:fileContent = strrep(Info{i}, ';', '\n');。[fileNameCode, '.txt']。fullfile构建完整文件路径。fopen将文件作为文本文件打开f = fopen(filename, 'wt');。使用:fileContentfprintffprintf(f, [fileContent, '\n']);'\n'fclose(f);代码示例:
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
Run Code Online (Sandbox Code Playgroud)
内容D1.txt例如:
40 11
15 41
我们可以直接使用表字段,直到我们必须写入文件为止。
由于我们将每一行写入单独的文件,因此我们必须迭代这些行。
strrep可以应用于多行:
T.Info = strrep(T.Info, ';', new_line); % Replace all ";" with new line character (characters).
Run Code Online (Sandbox Code Playgroud)
我们可以使用rowfun来创建没有循环的文件名。
代码使用了匿名lambda函数,语法比较复杂:
T2 = rowfun(@(x) {fullfile('E:', [x, '.txt'])}, T, 'InputVariables', 'Code', 'OutputVariableName', 'filename', 'ExtractCellContents', true);
Run Code Online (Sandbox Code Playgroud)
代码示例使用,和dlmwrite代替(用于显示不同的方法)。fopenfprintffclose
更新的代码示例:
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
Run Code Online (Sandbox Code Playgroud)