Reading 8x8 matrices from file into MATLAB
2 次查看(过去 30 天)
显示 更早的评论
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 hash following each 8x8 integer matrix.
I need to read each 8x8 matrix into a MATLAB program, process it, and then write the result that has the same format.
How do I do this 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. The problem is that this program which generates these matrices generates them as an 8x8 block and not a single line containing all 64 values.
I have writen a Python script part of which does something similar. I read a line, check if first character is hash, if not then I use split function to convert the line into a list along the comma characters. Then I convert the list of string into list of integers and add it into another variable which stores the matrices. I believe there should be a simpler way to do this in MATLAB.
1 个评论
dpb
2022-7-5
Can you not upgrade? There are several of the new(ish) i/o routines introduced later that are handy if not indispensible. For things like this readlines is great; lets you bring in a whole file and then parse it in memory trivially.
Alternatively, there's low-level stuff with textscan that goes "all the way back" with the caveat it takes generally a fair amount of fiddling to get the formatting correct.
回答(2 个)
dpb
2022-7-5
编辑:dpb
2022-7-6
Try something like
fid=fopen('yourfile.txt','r');
i=0;
C=[];
while ~feof(fid)
i=i+1;
C{i}=reshape(cell2mat(textscan(fid,'%f',64,"Delimiter",',','CollectOutput',1)),8,8).';
fgetl(fid);fgetl(fid);
end
fid=fclose(fid);
The two fgetl calls serve to resynch the file pointer to beginning of record and then read/skip the # after each block.
0 个评论
Walter Roberson
2022-7-5
filename = 'YourFile.csv';
S = fileread(filename);
blocks = regexp(S, '#', 'split');
fmt = [repmat('%f,', 1, 7), '%f'];
numeric_blocks = cellfun(@(s) cell2mat(textscan(s, fmt)), blocks, 'uniform', 0);
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Import and Export 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!