简体   繁体   English

在MATLAB / Octave中拆分数组并导出值

[英]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.

相关问题 分割数组并导出值,同时在matlab / octave中调整部分 - splitting array and export values while adjusting the sections in matlab / octave 如何不使用Matlab / Octave在数组中重复值 - How not to repeat values in array using matlab / octave 使用Matlab /八度音程进行数值分组,并且不重复在主数组中找到的值 - Numerical grouping using matlab / octave and not repeating values found in main array GNU Octave/Matlab:如何获取数组中三个最大值的索引? - GNU Octave/Matlab: How to get the index of the three biggest values in an array? 使用circshift octave / matlab进行数组操作 - array manipulation with circshift octave / matlab MATLAB中的数组块拆分 - Array block splitting in MATLAB MATLAB / Octave:使用索引数组递增数组 - MATLAB / Octave : Increment array with an array of indexes 如何在octave / matlab中找到多维数组中具有相同值的所有单元格 - How can I find all the cells that have the same values in a multi-dimensional array in octave / matlab 使用Circshift的Matlab /八度音阶进行数组操作 - Array manipulation with matlab / octave using circshift 我应该使用哪种类型的数组来组合数值和字符串值,并能够以八度/ matlab对其进行排序 - What type of array should I use to combine numerical values and string values and have the ability to sort them in octave/matlab
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM