Undefined Variable error for a variable that is already defined?

Hello all,
I have this code that I have been running to process DICOM images. I previously used it to process two folders (with the same folder structure, just diff name) successfully. Now all of a sudden I get this error "Unrecognized function or variable 'a'. Error in PrintProtocol (line 82) for i=1:size(a,1)". PrintProtocol is a function written by a mentor and I am not the author.
Going to line 82 of PrintProtocol I see this: "for i=1:size(a,1)
cd([foldersin{1} imgfolder slash a(i).name]);"
'a' is defined above in line 60 as "for i=1:size(d,1)
if (d(i).isdir == 1 && length(d(i).name) > 2)
a(l,1) = d(i,1);".
I am confused why it would work previously and then stop working now. To me a is defined in line 60, but am I missing something? I am happy to provide additional details. Thank you for your help.

3 Comments

I'm not sure about this question,but I think maybe you should debug it through setting breakpoint at line 60.And you can see if there is a variable of 'a'.Hope to help you.
It is inside an "if" statement. It may not be assigned if condition is not met. Put a break point on line 82 and run it again to check the value of a.
I put a breakpoint at line 60 and I am still seeing it saying a is an unknown variable. Is there something different I should be doing to define a? I looked above and 'l=1', but I don't see anywhere it specifically says "a=" so maybe a isn't really defined. But if that is true, why did this work for 2 other folders?

Sign in to comment.

 Accepted Answer

a is defined in a if statement.
if (d(i).isdir == 1 && length(d(i).name) > 2)
If it's false, either d is not a dir or is name is juste 1 charater, a won't be define. Are you sure your folders path are correct ?

14 Comments

I believe d is the correct directory path. D is a 3x1 struct and says isdir for 2 out of the 3. But you're right it looks like one folder is named '.' another '..' and the last 'reorganized'. Reorganized is the not dir.
I should add when I look at all the folder variables they are all correct and it is pulling the right cases from the folders I am targeting.
s is a structure array output by dir function. The two first elements are always current folder "." and parent folder "..".
Try to start your loop (indexed with i) at 3 instead of 1, then you'll skip those folders.
for i = 3:numel(d)
Good idea, but it didn't work. I used for i=3:numel(d), since d=dir'*', and a is still being undefined. I changed the name of the parent folder and now my struct is a 4x1 with 'DS_store' as the third element. So i tried i=4:numel and 3:numel neither worked.
Can you share the entire loop ?
This is what starts at line 60:
for i=4:size(d,1) %i=1:size(d,1)
if (d(i).isdir == 1 && length(d(i).name) > 2)
a(l,1) = d(i,1);
if (d(i,1).datenum > newestfold)
newestfold = d(i,1).datenum;
newestfoldt = d(i,1).date;
end;
l=l+1;
end;
if (~isempty(strfind(d(i).name,'protocol.txt')))
protdate = d(i,1).datenum;
protdatet = d(i,1).date;
end;
end;
I should add, I did try "numel" but also tried size function just to try.
I think I found one part of the issue. When the code works, D is a 19x1 struct with the different imaging protocols being the 19. So the struct is pointing to the wrong dir.
Just above line 60 it says "d = dir('*'); " what does the asterisk mean in this case?
The * mean everything in the folder, it is not necessary here. It can be helpful if you want just a certain type of file for instance d = dir('*.xls') will only return xls files in the structure.
Try this befor your loop. It will remove files and '.' and '..' folders from your structure d.
d = dir(ParentFolder);
d = d(d.isdir); % Only dir
d = d(3:end); % Remove '.' and '..' folders
What is the purpose of the loop ? Because variables newestfold, newestfoldt, protdate and protdatet are clearly unused.
The description of this section is to look at the timing and kind of series in the case. Index the series and then save them in a different folder for use in tractography. There is a lot more to the section than just the loop above, but it isn't producing an error when stepping through.
Adding those lines unfortunately didn't work. The struct is now a 0x1 containing just the top horizontal line defining the variables in the struct. I think the issue is the dir. When I check the other cases where this code works dir has all the folders in the parent folder, but here dir only has '.' '..' and 'reorganized' so for some reason the dir is set to the new "img" folder that is being created and not the parent folder. I found a variable called "dirname" which is set to the "img" folder but I cant find where it is located in the code.
If struct is empty, the dir function couldn't find any folder.
dir('*') look in the current folder for ony kind of file or folder. I'd rathher call it with an explicit folder
d = dir(MyFolderPath, '*')
% Is equivalent to
d = dir(MyFolderPath)
Yea I think I see the issue now, but I don't know the solution. The parent folder holds all the imaging with different protocols so therefore it has 19 cases in it. Then the code creates an "img" folder where it copies over them over and then does the rest of the processing there. For some reason the folders were not getting copied over, so the img folder was empty when it should have 19 folders in it. The dir was point to the right place, but it wasn't filled.
Ok, so PrintProtocol was not the issue, then can you close this topic (and accept the answer if you want) ?
If you are stuck with your folder generation, feel free to open an another question !
Yea it ended up being another function that is used before printprotocol that wasn't doing its job. Thank you for your help!

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2020a

Community Treasure Hunt

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

Start Hunting!