How to append in .mat file row or columnwise cellmatrix

前端 未结 2 781
广开言路
广开言路 2021-01-15 11:49

I am running a simulation where i generate huge 2d sparse matrices and hence i use FIND function to only store nonzero values with their indices.

Now for each itera

相关标签:
2条回答
  • 2021-01-15 12:26

    You can't append data to an existing variable with save. You need different variables:

    clear all
    filename = 'data.mat';
    save(filename) %// empty file, for now. We'll append variables within the loop
    for n = 1:10  
        A = rand(5);  
        [i,j,vals] = find(A);  
        varname = ['data' num2str(n)]; %// varname is 'data1', 'data2' etc
        assignin('base',varname,[i,j,vals]); %// create that variable
        save(filename, varname, '-append') %// append it to file
    end
    
    0 讨论(0)
  • 2021-01-15 12:32

    You can use matfile with cells as long as you are not trying to index into the actual cells. Remember that cells are still arrays, so you can access each cell using array indexing. For example:

    >> x = {'this', 'is', 'an', 'example'};
    >> x{4}
    
    ans =
    
    example
    
    >> x(4)
    
    ans = 
    
        'example'
    

    The following just initializes the data. Make sure to save it with the '-v7.3' tag, so that it supports efficient partial saving and loading.

    data = {};
    save('data.mat', 'data', '-v7.3');
    

    Now you can access your data with the matfile

    mf = matfile('data.mat', 'Writable', true);
    for n=1:10  
       A=rand(5);  
       [i,j,vals]=find(A);  
       data={[i,j,vals]};
       mf.data(end+1, 1) = data;
    end
    

    Reference: matfile documentation

    0 讨论(0)
提交回复
热议问题