Search code examples
matlabmatrixsegment

Matrix segmentation into files in Matlab


I have a very large matrix (M X N). I want to divide matrix into 10 equal parts (almost) and save each of them into a separate file say A1.txt, A2.txt, etc. or .mat format. How can I do this ?

Below is a code to divide a matrix into 10 equal parts and data_size is (M / 10).

for i=1:10
    if i==1
        data = DATA(1:data_size,:);
    elseif i==10
         data = DATA((i-1)*data_size+1:end,:);
    else
         data = DATA((i-1)*data_size+1: i*data_size,:);
    end

    save data(i).mat data  
    % What should I write here in order to save data into separate file data1.mat, data2.mat etc. 

end

Solution

  • You can use

    save(['data' num2str(i) '.mat'], 'data');
    

    where [ ] is used to concatenate strings and num2str to convert an integer to a string.