我有一个大数组,行数会有所不同,我想拆分并导出到多个文件。
我正在考虑使用reshape命令,但后来我意识到这需要具有相同行数的数组,但并非总是如此。
clear all, clc,clf,tic
num_elm = 11;
num_elm_split = 4; %Splits into columns
t = linspace(1, num_elm, num_elm)';
v = reshape(t, num_elm_split,[]); %Will split array into different columns
%'for' loop to split number of elements
for ii = 1:length(t(:, 1))
ii
end
示例:
如果我有一个包含11个值的数组
a = [1; 2; 3; 4; 5; 6; 7; 8; 9; 10; 11]
我希望将它分成每三个值并将数据输出到文件中。
ouput1.txt 会有 的 1 2 3
ouput2.txt 会有 的 4 五 6
ouput3.txt 会有 的 7 8 9
ouput4.txt 会有 的 10 11
答案 0 :(得分:1)
基本思想是这样做:
values = 1 : 11;
binSize = 3;
fileNum = 1;
n = numel(values);
for i = 1 : binSize : n
part = values(i : min(n, i + binSize - 1));
fprintf('File %d contains %s\n', fileNum, mat2str(part));
fileNum = fileNum + 1;
end
输出:
File 1 contains [1 2 3]
File 2 contains [4 5 6]
File 3 contains [7 8 9]
File 4 contains [10 11]
当然,您需要调整循环中的代码以实际将数据保存在磁盘上。
答案 1 :(得分:0)
这是代码......这可能有助于其他人
clear all, clc,clf,tic
filesoxplaylistStr='1soxplaylist.pls'; %playlistist filename fix
dirplstmp='/tmp/tmp/';
values = 1 : 11;
binSize = 3;
fileNum = 0;
n = numel(values);
for ii = 1 : binSize : n
part = values(ii : min(n, ii + binSize - 1))';
fileNum = fileNum + 1
%open file to write to
fidsoxpl = fopen(strcat(dirplstmp,filesoxplaylistStr), 'w'); %create and open file to write to for sox playlist join file
for jj=1:length(part)
part_val=part(jj,1) %gets individual filename or values
%create sox file to join
fprintf(fidsoxpl,'File%s=%s%s.wav',num2str(jj),dirplstmp,num2str(part_val,'%06d'));%create playlist data file
fprintf(fidsoxpl,'\n');
end
% close file to write to
fclose(fidsoxpl);
fn=strcat('test',num2str(fileNum,'%02d'));
%join files with sox
syscmd=strcat({'sox '},dirplstmp ,filesoxplaylistStr, {' '},dirplstmp,fn,{'.wav'});
system(syscmd);
%add freq to file name
end