Home > database >  Reading 8x8 integer matrix csv file MATLAB
Reading 8x8 integer matrix csv file MATLAB

Time:07-05

I have a csv file generated from another program which looks like this:

 45, 133, 148, 213,  65,  26,  22,  73
 84,  51,  41, 249,  25, 167, 102,  72
217, 198, 117, 123, 160,   9, 210, 211
230,  64,  37, 215,  91,  76, 240, 163
123, 169, 197,  16, 225, 160,  68,  65
 89, 247, 170,  88, 173, 206, 158, 235
144, 138, 188, 164,  84,  38,  67,  29
 98,  23, 106, 159,  96,   7,  77,  67
 
142, 140, 240,  56, 176,   0, 131, 160
241, 199,  96, 245, 213, 218,  51,  75
 22, 226,  81, 106,  94, 252, 252, 110
  0,  96, 132,  38, 189, 150, 162, 177
 95, 252, 107, 181,  72,   7,   0, 247
228, 207, 203, 128,  91, 158, 164, 116
 70, 124,  20,  37, 225, 169, 245, 103
103, 229, 186, 108, 151, 170,  18, 168

 52,  86, 244, 244, 150, 181,   9, 146
115,  60,  50, 162,  70, 253,  43,  94
201,  72, 132, 207, 181, 106, 136,  70
 92,   7,  97, 222, 149, 145, 155, 255
 55, 188,  90,  58, 124, 230, 215, 229
231,  60,  48, 150, 179, 247, 104, 162
 45, 241, 178, 122, 149, 243, 236,  92
186, 252, 165, 162, 176,  87, 238,  29

There is always a space following each 8x8 integer matrix.

I need to read each 8x8 matrix into a MATLAB program, generate an operation on it, and then write the result that has the same format. The result will be 8x8 matrix of floats, with space following each 8x8 matrix.

How do I do these 2 things in MATLAB R2017a? There are so many different functions with some depracated and some only available in later versions of MATLAB. I am not sure what do use to get the result I need.

CodePudding user response:

For MATLAB release 2019a and above, you can use readmatrix and writematrix like this:

M = readmatrix('mat24x8.csv');
r = 1:8;
M1 = m(r,:);
M2 = m(r 8,:);
M3 = m(r 16,:);

% Apply whatever operations you want ..
M1 = sqrt(M1);
M2 = sin(M2);
M3 = cos(M3);

% Finally save again ..
M = [M1; M2; M3]
M = writematrix(M, 'new24x8.csv');

These functions are better than older ones like M = csvread(filename) and csvwrite(filename,M) because it preserves data accuracy. The file extension for older versions is .dat. Also note that white space has no meaning here, so, if you want each matrix stored separately, just save three files.

CodePudding user response:

you may use this approach

% read csv in older Matlab 
m1 = csvread("test.csv");

% random operation to get some floats
m2  = m1 / 17 ; 

% 
newMatrix =   ones(8,8);
newMatrix = string( newMatrix );

for i =1 : height(r) 

    for k = 1 : 8 
       
        newMatrix(i,k) = string(g(i,k));
    end 

end 


   cell2csv('testFile.csv' ,newMatrix , ", "  )


% function below created by Sylvain Fiedler modified by Rob Kohr
%https://www.mathworks.com/matlabcentral/fileexchange/7601-cell2csv

 

 function cell2csv(filename,cellArray,delimiter)
% Writes cell array content into a *.csv file.
% 
% CELL2CSV(filename,cellArray,delimiter)
%
% filename      = Name of the file to save. [ i.e. 'text.csv' ]
% cellarray    = Name of the Cell Array where the data is in
% delimiter = seperating sign, normally:',' (it's default)
%
% by Sylvain Fiedler, KA, 2004
% modified by Rob Kohr, Rutgers, 2005 - changed to english and fixed delimiter
if nargin<3
    delimiter = ',';
end
datei = fopen(filename,'w');
for z=1:size(cellArray,1)
    for s=1:size(cellArray,2)
        
        var = eval(['cellArray{z,s}']);
        
        if size(var,1) == 0
            var = '';
        end
        
        if isnumeric(var) == 1
            var = num2str(var);
        end
        
        fprintf(datei,var);
        
        if s ~= size(cellArray,2)
            fprintf(datei,[delimiter]);
        end
    end
    fprintf(datei,'\n');
end
fclose(datei);
 end

  • Related