How do I output the same single parameter for a set of different files?
조회 수: 1 (최근 30일)
이전 댓글 표시
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
채택된 답변
Stephen23
2018년 10월 31일
편집: Stephen23
2018년 10월 31일
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
Stephen23
2018년 10월 31일
편집: Stephen23
2018년 10월 31일
"'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.
추가 답변 (1개)
madhan ravi
2018년 10월 31일
댓글 수: 5
madhan ravi
2018년 10월 31일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Debugging and Analysis에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!