1

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.

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

5 Comments

While I'm critiquing answers, please add a description of what your code does. Code-only answers are considered low-quality. I realize that you implicitly acknowledged that you're using a MATLAB File Exchange function in your code, but it might be helpful to state that explicitly. Does your code do what the OP asks: read/write multiple 8x8 matrices in csv format with a blank line separating each matrix? Why have you used a JavaScript/HTML/CSS code snippet to format MATLAB code instead of a normal code block?
it is very straightforward. I do not understand what your function is in this Stackoverflow Q-A problem though. @beaker
answer for the second part of your question is that I think there should be a better design for stackoverflow than this one. In addition to graphic design, technically there are still some issues when we try to use normal code block. It does not wrap code block properly. Therefore it is convenient to use snippet tool instead, although it does not run any matlab code yet. Another improvement possibility.
I get this error: Error using dlmread (line 147) Mismatch between file and format character vector. Trouble reading 'Numeric' field from file (row number 9, field number 1) ==> #\n Error in csvread (line 48) m=dlmread(filename, ',', r, c);
The empty line causes the program to break
1

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.

Update: To work in version R2017a of MATLAB, you should use the csvread(filename)andcsvwrite(filename,M)` I mentioned above in the same way as below.

M = csvread('myFile.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];
csvwrite('myFile.csv',M);

No method of the above will work for arbitrary separators in the csv file other than blank lines.

5 Comments

How does this apply to 2017a?
I mentioned the functions to be used for older versions than 2019a.
Only to say don't use them. That doesn't really answer the question "How do I do these 2 things in MATLAB R2017a?", does it?
Thanks, if I read the data, the entire data is read into a single variable and then needs to be separated. The separation process is actually not that difficult. If the space character is replaced with something else like hash, then the readmatrix function falls over. If the separate is a character other than just an empty line between the two matrices, is it still possible to make the readmatrix function work with it?
I get this error "Undefined function or variable 'readmatrix'."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.