Changes made to the windows folders are not reflected in brainstorm database causing errors

Hello,
I have been getting the following error

Loading current protocol...
Index exceeds array bounds.

Error in node_create_db_studies>@(c)c{1} (line 149)
[tmp__, iStudiesSortedNonRaw] = sort(cellfun(@(c)c{1}, {ProtocolStudies.Study(isNonRaw).Condition},
'UniformOutput', 0));

Error in node_create_db_studies (line 149)
[tmp__, iStudiesSortedNonRaw] = sort(cellfun(@(c)c{1}, {ProtocolStudies.Study(isNonRaw).Condition},
'UniformOutput', 0));

Error in panel_protocols>UpdateTree (line 365)
case 'StudiesSubj', defNode = node_create_db_studies(nodeRoot, 'subject');

Error in panel_protocols (line 44)
eval(macro_method);

Error in db_load_protocol (line 89)
panel_protocols('UpdateTree');

Error in gui_brainstorm>SetCurrentProtocol (line 1020)
db_load_protocol(iProtocol);

Error in gui_brainstorm (line 33)
eval(macro_method);

Error in bst_startup (line 526)
gui_brainstorm('SetCurrentProtocol', GlobalData.DataBase.iProtocol);

Error in brainstorm (line 154)
bst_startup(BrainstormHomeDir, 1, BrainstormDbDir);

This I believe primarily happened because I copied and pasted brainstormstudy.mat file in a subject's data folder. Once I recognised this error I deleted the file. But when I reopen brainstorm this error still pops up and also the ProtocolStudies.Study struct and it's fields still read that file although it is has been permanently deleted from my computer.

Please help with this issue.

Thank you for your time.

Try reloading your protocol:
go to the anatomy view of the protocol so you don't get this error, right-click on the top node in the database explorer > Reload.

When I perform the above reload, I get the following error:

DB_FIX> Missing subject: ""...
Array indices must be positive integers or logical values.

Error in db_fix_protocol (line 183)
SubjectFile = ProtocolSubjects.Subject(iSubjectRm).FileName;

Error in db_reload_database (line 65)
isError(i) = db_fix_protocol();

Error in panel_protocols>ReloadNode (line 555)
isError = db_reload_database('current');

Error in panel_protocols (line 44)
eval(macro_method);

Error in tree_callbacks>@(h,ev)panel_protocols('ReloadNode',bstNodes(1)) (line 2291)
gui_component('MenuItem', jPopup, [], 'Reload', IconLoader.ICON_RELOAD, [], @(h,ev)panel_protocols('ReloadNode',
bstNodes(1)));

We would have to remove the extra file present in ProtocolStudies.Study struct:

I tried to do something like this by editing the db_fix_protocol.m

% List of subjects referenced in the studies
listSubjFile = unique({ProtocolStudies.Study.BrainStormSubject});
for iSubj = 1:length(listSubjFile)
SubjName = bst_fileparts(listSubjFile{iSubj});
% Skip default subject
if strcmpi(SubjName, bst_get('DirDefaultSubject'))
continue;
end
allSubj{end+1} = SubjName;
if ~file_exist(bst_fullfile(ProtocolInfo.SUBJECTS, listSubjFile{iSubj}))
missingSubj{end+1} = SubjName;

    if isempty(SubjName)
        % This means there is some fault in the way files are present
        % in the database. Mostly a file was added that was not
        % supposed to be there directly in the subject folder
        % for example a brainstormstudy.mat
        for unwanted_file = 1:1:length(ProtocolStudies.Study)
            if strcmpi((ProtocolStudies.Study(unwanted_file).BrainStormSubject),...
                    listSubjFile{iSubj})
                
                ProtocolStudies.Study(unwanted_file) = [];
                bst_set('ProtocolStudies', ProtocolStudies);
                listSubjFile = unique({ProtocolStudies.Study.BrainStormSubject});
                iSubj = iSubj - 1;
                SubjName = bst_fileparts(listSubjFile{iSubj});
                missingSubj = {};
                break
            end
        end
                   
                

        
        
        
    end
    
end

end

It helps but then the brainstorm continues to show that progress bar saying fixing database errors and when I reopen brainstorm the error that I had originally posted reappears

This is going to difficult help you fix this database remotely.
Don't you have a backup of your work somewhere? (If not it would be a good time to set up one...)

You could try to identify the offending folders, and delete them both from the anat and the data folders. Then:

  • detach your protocol from Brainstorm ( File > Delete protocol > Only detach from database)
  • delete the data/protocol.mat file
  • Add your protocol again: File > Load protocol > Load from folder

Thanks for your help Francois, but this is not working for me.
I will recreate my database, fortunately I have a backup which works fine.
Is there a built-in function to create a folder with brainstormstudy.mat file already in it ?
That will reduce this error of copy pasting the file accidentally in the wrong folder.

You can use the function db_add_condition.m, see the header of the file for help.
https://github.com/brainstorm-tools/brainstorm3/blob/master/toolbox/db/db_add_condition.m

Do you really need to copy-paste files in the first place?
Manipulating Brainstorm files manually is not recommended. If you are not very confident with what you are doing with the structure of the files, I recommend you only use the database explorer or the processes in the Files menu to manipulate files. If you are more confident with Matlab programming, you can try using the lower-level database manipulation functions (in folder brainstorm3/toolbox/db, or in functions bst_get/bst_set).