changing the dimension representation

I have 8 face images (2 diff people with 4 diff expressions each). To arrange the data as multidimensional array, first i put 'people' in rows and 'expressions' in columns using the following code (p=people, e=expressions):
j=2;
k=4;
mydata = cell(j,k);
for p = 1:j
for e = 1:k
fname = sprintf('%d%02d.txt' , p, e);
fid = fopen(fname);
Next,if i want to change the 'expressions' be in the rows and 'people' in column, how can i do that without changing the file names. I name the image as 101-104 for the first person with four expressions and 201-204 for the second person.I know that first i will change :
j=4;
k=2;
mydata = cell(j,k);
for e = 1:j
for p = 1:k
fname = sprintf('%d%02d.txt' , e, p);
fid = fopen(fname);
but i don't know how to change the '%d%02d.txt'. Anyone have an idea? Or does the changes in dimension representation can be done in only one file? I mean without creating another m file.

 Accepted Answer

First, a piece of advice, use variable names that have meaning, preferably full words. It's impossible to tell from their name alone what j, k, e, p represent. personcount, expressioncount, person, expression, are much clearer. Even mydata is not very good, it doesn't say what kind of data it holds. I assume images of people expression, so imgexpression would be better.
Second, there is nothing in the snippet you've posted that depends on the order of the dimension. If you swap the order of the dimensions, the only two lines that need to change are the array declaration and the assignment to it (which you haven't shown). Certainly not the order of the loops, nor the sprintf call. So:
expressioncount = 4;
personcount = 2;
imgexpression = cell(personcount, expressioncount);
%or if you swap:
%imgexpression = cell(expressioncount, peoplecount);
%the loop order does not matter one bit
for person = 1:personcount
for expression = 1:expressioncount
fname = sprintf('%d%02d.txt', person, expression); %according to your description this is what you should be using
fid = fopen(fname);
%... some code
%at some point you need to do the assignment
imgexpression{person, expression} = ...
%if you swap:
%imgexpression{expression, person} = ...
end
end

3 Comments

Thank you very much Guillaume for your advice. I really appreciate that. This is my code:
personcount = 2;
expressioncount = 4;
imgexpression = cell(personcount, expressioncount);
for person = 1:personcount
for expression = 1:expressioncount
fname = sprintf('%d%02d.txt', person, expression);
fid = fopen(fname);
A = fscanf(fid, '%f %f %f');
mydata{person, expression} = A
B = cat( 3, mydata{:} );
fclose(fid);
end
end
Using the above code, i got a correct arrangement where person in the first and second rows, and expressions in the first column until fourth column. However when i change to this:
personcount = 2;
expressioncount = 4;
imgexpression = cell(expressioncount, personcount);
for expression = 1:expressioncount
for person = 1:personcount
fname = sprintf('%d%02d.txt', expression, person);
fid = fopen(fname);
A = fscanf(fid, '%f %f %f');
mydata{expression,person} = A
B = cat( 3, mydata{:} );
fclose(fid);
end
end
i got an incorrect arrangement where person still in the first and second rows with only two expressions in the first and second columns. How i should do to make expressions in the rows and person in columns?Thank you for your help.
Well, of course you get exactly the same result with the two codes since the only thing you've done is effectively swapped the names of the variables. You certainly shouldn't swap anything in the call to sprintf. So,
case 1:
personcount = 2;
expressioncount = 4;
imgexpression = cell(personcount, expressioncount);
for person = 1:personcount
for expression = 1:expressioncount
fname = sprintf('%d%02d.txt', person, expression);
fid = fopen(fname);
%what you were doing in the loop didn't make any sense
imgexpression{person, expression} = fscanf(fid, '%f %f %f')
fclose(fid);
end
end
%concatenation in 3rd dimension only after the loop
%as explained use better variable name than B
imgexpression = cat(3, imgexpression{:});
case 2, only two things to swap as explained
personcount = 2;
expressioncount = 4;
imgexpression = cell(expressioncount, personcount);
for person = 1:personcount
for expression = 1:expressioncount
fname = sprintf('%d%02d.txt', person, expression);
fid = fopen(fname);
%what you were doing in the loop didn't make any sense
imgexpression{expression, person} = fscanf(fid, '%f %f %f')
fclose(fid);
end
end
%concatenation in 3rd dimension only after the loop
%as explained use better variable name than B
imgexpression = cat(3, imgexpression{:});
Thank you very much Guillaume!! I understand now your first explanation and got it correctly.

Sign in to comment.

More Answers (0)

Categories

Find more on Convert Image Type in Help Center and File Exchange

Asked:

on 22 May 2016

Commented:

on 24 May 2016

Community Treasure Hunt

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

Start Hunting!