Info

La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.

How this code writes in Cell array ?

1 visualización (últimos 30 días)
subha
subha el 27 de Jun. de 2014
Cerrada: MATLAB Answer Bot el 20 de Ag. de 2021
As per my understanding, the below code does the following.
i) create 10 cells
ii) In each cell a file called digit is opened in write mode.
could somebody elaborate it in further?
n=2;
Df = cell (1,10);
for d=0:9,
Df{d+1} = fopen(['digit' num2str(d) '.ascii'],'w');
end;
for i=1:60,
fprintf('.');
rawimages = fread(f,28*28*n,'uchar');
rawlabels = fread(g,n,'uchar');
rawimages = reshape(rawimages,28*28,n);
for j=1:n,
fprintf(Df{rawlabels(j)+1},'%3d ',rawimages(:,j));
fprintf(Df{rawlabels(j)+1},'\n');
end;
end;
for d=0:9,
fclose(Df{d+1});
D = load(['digit' num2str(d) '.ascii'],'-ascii'); % load digit from the datafile, treat digit as ASCII file
fprintf('%5d Digits of class %d\n',size(D,1),d);
save(['digit' num2str(d) '.mat'],'D','-mat'); %save digits in matlab format
end;

Respuestas (1)

Geoff Hayes
Geoff Hayes el 27 de Jun. de 2014
The code does not write in the cell array. Each cell of the array is populated with a file identifier to a file named digitX.ascii where X is one of 0,1,...,9 that has been opened for writing.
These identifiers (well at least two of them since n is equal to two) are used to write data from rawlabels and the first and second columns of rawimages on each iteration of the outer for loop. See fread and reshape for details on these two function.
The last for loop then closes each file, loads the data from each file into the local variable D, and then saves that data to a mat file.
  2 comentarios
subha
subha el 29 de Jun. de 2014
Editada: subha el 29 de Jun. de 2014
Thanks. fread read datas from f file with the size 28*28*n and precision- uchar. i think below lines are same as the previous. What do you feel?
rawimages = fread(f,[28*28,n],'uchar');
rawlabels = fread(g,n,'uchar');
when i run the above file, i am getting results like this as per last for loop. since i have given n=2, 60*2=120. So total it reads 120 images. Where it is mentioned how many digits each digitx.ascii should store?
17 Digits of class 0
19 Digits of class 1
8 Digits of class 2
13 Digits of class 3
12 Digits of class 4
6 Digits of class 5
12 Digits of class 6
12 Digits of class 7
8 Digits of class 8
13 Digits of class 9
Also i want to know, what these lines are doing, could you please elobarate
for j=1:n,
fprintf(Df{rawlabels(j)+1},'%3d ',rawimages(:,j));
fprintf(Df{rawlabels(j)+1},'\n');
end;
Geoff Hayes
Geoff Hayes el 1 de Jul. de 2014
Subha - I don't understand what you mean by I think below lines are same as previous. Previous to what? They are the same lines from your code (without any changes). The f is file descriptor to a file that was opened elsewhere; the g is another file descriptor to some file opened elsewhere (since code is not present for this).
As for there being 120 images, I suppose that could be true. I can't say for certain since I don't know what kind of data the f "points" to. But since we read in 28*28*2 unsigned char (bytes) and then reshape them into a (28*28)x2 matrix, and then write each column to files (based on rawlabels), then, yes, 120 columns of data will be written to file.
Note that even though rawimages is a 784x2 matrix and that we write each column to file, the column is actually written as a row to the file
fprintf(Df{rawlabels(j)+1},'%3d ',rawimages(:,j));
fprintf(Df{rawlabels(j)+1},'\n');
So every time we write a column of data to a file given its label, we actually write an additional row of data to a file.
The final for loop closes each file, and loads the contents of that file into the local variable D
D = load(['digit' num2str(d) '.ascii'],'-ascii');
D is now a mx784 matrix where m is the number of rows in that file, which corresponds to the number of columns of data that we wrote previously to that file.
The statement
fprintf('%5d Digits of class %d\n',size(D,1),d);
gets the number of rows of D from size(D,1) and prints out the
17 digits of ..
19 digits of ..
where there are 17 rows in D loaded from the first file, 19 rows in D loaded from the second file, etc.

La pregunta está cerrada.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by