How do I output the same single parameter for a set of different files?

I have a code that I want to use to extract a particular parameters from all files in my directory with a .mat ending. I have set a loop to calculate the parameter R for each of the input files but I have made a mistake somewhere which means it records the value from the final loop for every element in the array. I'm not sure where I'm going wrong as the parameter I'm calculating is part of the loop. I know this is a simple question but I can't find an answer to it anywhere so help would be appreciated.
files = dir('*.mat');
Rarray = zeros(1,19);
for file = files'
matfile = load(file.name);
A4 = cf*A+B+cf*C;
B4 = cf*C+D+cf*E;
C4 = cf*E+F+cf*G;
D4 = cf*G+H+cf*A;
V = 2*complex(A4-C4,B4-D4)./(A4+B4+C4+D4);
for m=1:19
R = sum(abs(V))/length(V);
Rarray(m)=R
end
end

2 commentaires

Stephen23
Stephen23 le 31 Oct 2018
Modifié(e) : Stephen23 le 31 Oct 2018
You don't use the badly named matfile anywhere inside the code. Is this intentional?
You calculate exactly the same R value 19 times in a loop. Is this intentional?
This was meant to be a way of loading each file but I realise something is going wrong with that part.

Connectez-vous pour commenter.

 Réponse acceptée

Stephen23
Stephen23 le 31 Oct 2018
Modifié(e) : Stephen23 le 31 Oct 2018
If you want to save the values from both loops, then you will need to use indexing corresponding to both loops. Currently you use the index m to store values from the inner loop, but you completely overwrite all variables on each iteration of the outer loop because you do not use any indices corresponding to that loop. Try something like this:
cf = 1;
D = 'directory where the files are saved';
S = dir(fullfile(D,'*.mat'));
N = numel(S)
R = zeros(N,19); % size (outerloop,innerloop)
for ii = 1:N
T = load(fullfile(D,S(ii).name));
A4 = cf*T.A + T.B + cf*T.C;
B4 = cf*T.C + T.D + cf*T.E;
C4 = cf*T.E + T.F + cf*T.G;
D4 = cf*T.G + T.H + cf*T.A;
V = 2*complex(A4-C4,B4-D4)./(A4+B4+C4+D4);
for jj = 1:19
R(ii,jj) = mean(abs(V)); % simpler
% ^^ ^^ (outerloop,innerloop)
end
end
Note how I used the loaded data. I tested this using the files you uploaded to your other question, and it gives this output:
>> R
R =
0.22448 0.22448 0.22448 0.22448 0.22448 0.22448 0.22448 0.22448 0.22448 0.22448 0.22448 0.22448 0.22448 0.22448 0.22448 0.22448 0.22448 0.22448 0.22448
0.28552 0.28552 0.28552 0.28552 0.28552 0.28552 0.28552 0.28552 0.28552 0.28552 0.28552 0.28552 0.28552 0.28552 0.28552 0.28552 0.28552 0.28552 0.28552
To be honest I don't understand why you want to repeat exactly the same calculation 19 times, but that is what your code does. Quite probably the inner loop is completely unnecessary, but this depend on your actual processing and algorithm.

5 commentaires

This still seems to have a problem with the values of A, B, etc. I've attached a file to show what the formatting of the files is, because for some reason it isn't reading these values.
I wasn't trying to do the same calculation 19 times, I was trying to calculate R for each of the files. I haven't been able to get that to work correctly though
"I wasn't trying to do the same calculation 19 times, I was trying to calculate R for each of the files"
Then get rid of the inner loop, it is completely pointless:
cf = 1;
D = 'directory where the files are saved';
S = dir(fullfile(D,'*.mat'));
N = numel(S)
R = zeros(N,1);
for ii = 1:N
T = load(fullfile(D,S(ii).name));
A4 = cf*T.A + T.B + cf*T.C;
B4 = cf*T.C + T.D + cf*T.E;
C4 = cf*T.E + T.F + cf*T.G;
D4 = cf*T.G + T.H + cf*T.A;
V = 2*complex(A4-C4,B4-D4)./(A4+B4+C4+D4);
R(ii) = mean(abs(V)); % simpler
end
which gives this R output:
>> R
R =
0.22448
0.28552
"...because for some reason it isn't reading these values."
Use my code exactly, it is not the same as yours. Note how it refers to the loaded data structure T within your calculations, e.g. T.A, T.B, etc..
JJH
JJH le 31 Oct 2018
Modifié(e) : JJH le 31 Oct 2018
Thanks for this, I'm still getting a problem though, I'm getting the error 'Reference to non-existant field A' even though there is clearly an A in the files. I'm not sure it's loading the files correctly. I have entered the directory that the files are in for D so I'm not sure what the problem is. EDIT: One of the files in my list had become corrupted. I am now able to extract the data I want.
"'Reference to non-existant field A' "
It seems like one of your files actually does NOT contain an A field. Possibly you have other .mat files in the same directory, or one of the .mat files is not written correctly. In either case, it is clearly caused by T not having the A field. You can figure out which file it is by simply using the most recent loop index and the dir structure:
S(ii).name
Then check that file.

Connectez-vous pour commenter.

Plus de réponses (1)

5 commentaires

There isn't a problem with the numbering of the files, they're number u1.mat, u2.mat etc. The problem is that when the loop runs the output parameter is always for the final file in the set.
for file = 1:numel(files')
Because didn’t even run as you think that’s why I told you to read the link but you didn’t!
I'm sorry but I don't understand what you're trying to say. The code you suggested gives an error in the load(file.name) command and I don't see anything about the code you wrote in the link you sent.
files = dir('*.mat');
Rarray = zeros(1,19);
for n = 1:numel(files')
matfile1(n) = load(file.name); %matfile is an inbuilt function so I renamed it
A4 = cf*A+B+cf*C;
B4 = cf*C+D+cf*E;
C4 = cf*E+F+cf*G;
D4 = cf*G+H+cf*A;
V = 2*complex(A4-C4,B4-D4)./(A4+B4+C4+D4);
for m=1:19
R = sum(abs(V))/length(V);
Rarray(m)=R
end
end
This gives the error 'Dot indexing is not supported for variables of this type'. I think the problem might be to do with the format of the file itself.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Variables dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by