[英]Splitting array and export values in MATLAB / Octave
I have a large array where the amount of rows will vary, and I would like to split up and export to multiple files. 我有一个很大的数组,行数会有所不同,我想拆分并导出到多个文件。
I was thinking of using the reshape command, but then I realized for this to work the arrays needed to have the same number of rows which will not always be the case. 我当时在考虑使用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
Example: 例:
If I have an array of 11 values 如果我有11个值的数组
a = [1; 2; 3; 4; 5; 6; 7; 8; 9; 10; 11]
And I want it be split up on every three values and output the data to a file. 我希望将其每三个值分割一次,然后将数据输出到文件中。
ouput1.txt would have in it 1 2 3 ouput1.txt将包含其中1 2 3
ouput2.txt would have in it 4 5 6 ouput2.txt将包含其中4 5 6
ouput3.txt would have in it 7 8 9 ouput3.txt将包含在其中7 8 9
ouput4.txt would have in it 10 11 ouput4.txt将包含在其中10 11
I know I could use the split command in Linux, but I'm trying to use only MATLAB/ Octave code. 我知道我可以在Linux中使用split命令,但是我试图仅使用MATLAB / Octave代码。
The basic idea is to do it like this: 基本的想法是这样做:
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
Output: 输出:
File 1 contains [1 2 3]
File 2 contains [4 5 6]
File 3 contains [7 8 9]
File 4 contains [10 11]
You need to adapt the code in the loop to actually save the data on disk, of course. 当然,您需要修改循环中的代码以实际将数据保存在磁盘上。
Here's the code...this may help someone else out 这是代码...这可能有助于其他人
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.