Info
Questa domanda è chiusa. Riaprila per modificarla o per rispondere.
How this code writes in Cell array ?
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
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;
0 Commenti
Risposte (1)
Geoff Hayes
il 27 Giu 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 Commenti
Geoff Hayes
il 1 Lug 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.
Questa domanda è chiusa.
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!