MATLAB: How to make ‘for’ loops to represent rows and columns

cell arraysdigital image processing

i have a 3D face image dataset. I want to create a cell where rows represent diff people and columns represent diff expressions.
j=2;
k=4;
mydata = cell(j,k);
for p = 1:j
for e = 1:k
fname = sprintf('%0%d.txt' , p, e);
fid = fopen(fname);
A = fscanf(fid, '%f %f %f');
mydata{p,e} = A
fclose(fid);
end
end
For my above code, j represent no of people(this is an example only,real dataset contains 50 people), and k is no of expressions for each people. I named the image as 101 until 104 for the first people, and 201 until 204 for the second people. 'p' represent people and 'e' is expression. However i got an error saying that "error using fscanf.Invalid file identifier". I think the problem arise because it cannot detect which image for 'p' and which one for'e'. But i don't know how to solve this.

Best Answer

You get "error using fscanf.Invalid file identifier" because the fid in the fscanf call is invalid. The fid is invalid because fopen failed. When dealing with i/o operations, it's always a good idea to check that it succeeds. e.g.:
fid = fopen(fname);
if fid == -1
%file failed to open
error('Failed to open %s', fname);
end
Most likely, the fopen failed because your filename is invalid. That would be because your format string in the sprintf call is incorrect. Possibly, you meant:
fname = sprintf('%d%02d.txt', p, e);
This will create '105' out of p = 1, e = 5; '114' out of p = 1, e = 14; '1005' out of p = 10, e = 5; and '1014' out of p = 10, e = 14.