Reference to non-existent field error
이전 댓글 표시
Hi there, I am new at MATLAB and currently trying to extract data from a particular subfolder from every subject and to put it all into an excel file
basically i want to retrieve mean times which is calculated from taking the mean of values in delay series variable (location: parent_directory > containing many subject folders labelled img0001, img0002 etc > each subject folder contains a .mat file called stopchoicert.mat > variable delayseries can be found here)
D = 'C:\Users\extlzx\Documents\MATLAB\Behavioural Data and script\behavioural data\';
S = dir(fullfile(D,'img*'));
for k = 264 %to access folder for subject number 264, usually i would put this as a loop from 1 to 300 but just for testing i put 264
SSRT = fullfile(D,S(k).name,'stopchoicert.mat');
if exist(SSRT,'file')==2;
T = load(SSRT);
meanssd = mean(delayseries); %delayseries is a 160X160 double(?) but only the first column contains value of interest. the rest are zeros. hence i took the mean and specified the first column and first row of meanssd which would contain my value that i wish to extract
S(k).data = T.meanssd(1,1);
end
end
writetable(struct2table(S), 'extracteddata2.xlsx')
Why do i get the error of:
Reference to non-existent field 'meanssd'.
Error in ssrtscript (line 8)
S(k).data = T.meanssd(1,1);
am I not creating new variable meanssd @ line 8? I do see variable meanssd in my workspace though. Encountered similar problems before but have been unable to resolve
댓글 수: 4
Walter Roberson
2020년 1월 7일
T is the result of your load() operation. Whatever was in the .mat file, there was no variable named meanssd there.
for k = 264 %to access folder for subject number 264, usually i would put this as a loop from 1 to 300 but just for testing i put 264
Be careful there. Unless there are no other img* files or folder, and unless the files are numbered with leading 0's, entry number 264 in the file list will likely not contain img264 . If you do not pad the names with leading 0s, the order would probably be img1, img10, img100, img101, img102 ... img109, img11, img110, img111 ... img119, img12, img120... img129, and so on to img190 to img199, after which there would be img2 then img20 then img200 then img201 to img209, img21, img210 to img219, and so on to img299, followed by img3, img30, img300, img31 to img39, img4, img40 to img49, img5, img50 to img59, img6, img60 to img69, img7, img70 to img79, img8, img80 to img89, img9, img90 to img99 and that would be the last one. The 264'th in this order is not going to be img264 .
Look at these three lines of code:
T = load(SSRT); % load file data into structure T.
meanssd = mean(delayseries); % allocate to variable MEANSSD, which you totally ignore.
S(k).data = T.meanssd(1,1); % access field MEANSSD of structure T.
You do not allocate to a field with that name, so it would have to exist in the file in order to be in that structure, which apparently is not the case. If the structure T does not have that field then trying to refer to it like that will throw an error.
"am I not creating new variable meanssd?"
Yes, but you never use it for anything.
zexi Leong
2020년 1월 7일
"Would this make more sense?"
Not really, as now you load some file data into a structure T, which you then totally ignore (because that variable is never accessed, and it is completely reallocated two lines later).
As you have not explained what data you expect to be in S, it is hard to make any suggestions what you should be doing. Perhaps you are trying to do something like this:
T = load(SSRT);
V = mean(T.delayseries);
S(k).mssd = V(1,1);
답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Programming에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!