diff --git a/toolbox/core/bst_memory.m b/toolbox/core/bst_memory.m index 245b3f1e1..b4995b66a 100644 --- a/toolbox/core/bst_memory.m +++ b/toolbox/core/bst_memory.m @@ -795,9 +795,6 @@ function LoadChannelFile(iDS, ChannelFile) GlobalData.DataSet(iDS).DataFile = file_short(DataFile); GlobalData.DataSet(iDS).Measures = Measures; - % ===== LOAD CHANNEL FILE ===== - LoadChannelFile(iDS, ChannelFile); - % ===== Check time window consistency with previously loaded data ===== if isTimeCheck % Update time window @@ -814,25 +811,18 @@ function LoadChannelFile(iDS, ChannelFile) return; % Otherwise: unload all the other datasets else - % Save newly created dataset - bakDS = GlobalData.DataSet(iDS); - % Unload everything - UnloadAll('Forced'); - % If not everything was unloaded correctly (eg. the user cancelled half way when asked to save the modifications) - if ~isempty(GlobalData.DataSet) - % Unload the new dataset - UnloadDataSets(iDS); - iDS = []; - return; + iDS = UnloadOtherDs(iDS); + if isempty(iDS) + return end - % Restore new dataset - GlobalData.DataSet = bakDS; - iDS = 1; % Update time window isTimeCoherent = CheckTimeWindows(); end end end + + % ===== LOAD CHANNEL FILE ===== + LoadChannelFile(iDS, ChannelFile); % ===== UPDATE TOOL TABS ===== if ~isempty(iDS) && strcmpi(GlobalData.DataSet(iDS).Measures.DataType, 'raw') @@ -1251,14 +1241,24 @@ function ReloadStatDataSets() %#ok isTimeCoherent = CheckTimeWindows(); % If loaded results are not coherent with previous data if ~isTimeCoherent - % Remove it - GlobalData.DataSet(iDS).Results(iResult) = []; - iDS = []; - iResult = []; - bst_error(['Time definition for this file is not compatible with the other files' 10 ... - 'already loaded in Brainstorm.' 10 10 ... - 'Close existing windows before opening this file, or use the Navigator.'], 'Load results', 0); - return + res = java_dialog('question', [... + 'The time definition is not compatible with previously loaded files.' 10 ... + 'Unload all the other files first?' 10 10], 'Load results', [], {'Unload other files', 'Cancel'}); + % Cancel: Unload the new dataset + if isempty(res) || strcmpi(res, 'Cancel') + UnloadDataSets(iDS); + iDS = []; + return; + % Otherwise: unload all the other datasets + else + iDS = UnloadOtherDs(iDS); + if isempty(iDS) + iResult = []; + return + end + % Update time window + isTimeCoherent = CheckTimeWindows(); + end end end % Update TimeWindow panel, if it exists @@ -1982,13 +1982,31 @@ function LoadResultsMatrix(iDS, iResult) if isempty(iDS) && isempty(Mat.Events) iDS = GetDataSetStudy(sStudy.FileName); end - % Create dataset - if isempty(iDS) + % Check time against existing DS + isTimeOkDs = 1; + if ~isempty(iDS) && (length(Mat.Time) >= 2) + % Save measures information if no DataFile is available + if isempty(GlobalData.DataSet(iDS).Measures) || isempty(GlobalData.DataSet(iDS).Measures.Time) + GlobalData.DataSet(iDS).Measures.Time = double(Mat.Time([1, end])); + GlobalData.DataSet(iDS).Measures.SamplingRate = double(Mat.Time(2) - Mat.Time(1)); + GlobalData.DataSet(iDS).Measures.NumberOfSamples = length(Mat.Time); + elseif (abs(Mat.Time(1) - GlobalData.DataSet(iDS).Measures.Time(1)) > 1e-5) || ... + (abs(Mat.Time(end) - GlobalData.DataSet(iDS).Measures.Time(2)) > 1e-5) || ... + ~isequal(length(Mat.Time), GlobalData.DataSet(iDS).Measures.NumberOfSamples) + isTimeOkDs = 0; + end + end + % Create dataset if not existent or different time definition + if isempty(iDS) || ~isTimeOkDs % Create a new DataSet only for results iDS = length(GlobalData.DataSet) + 1; GlobalData.DataSet(iDS) = db_template('DataSet'); GlobalData.DataSet(iDS).SubjectFile = file_short(sStudy.BrainStormSubject); GlobalData.DataSet(iDS).StudyFile = file_short(sStudy.FileName); + % Save measures information + GlobalData.DataSet(iDS).Measures.Time = double(Mat.Time([1, end])); + GlobalData.DataSet(iDS).Measures.SamplingRate = double(Mat.Time(2) - Mat.Time(1)); + GlobalData.DataSet(iDS).Measures.NumberOfSamples = length(Mat.Time); end % Make sure that there is only one dataset selected iDS = iDS(1); @@ -1996,26 +2014,28 @@ function LoadResultsMatrix(iDS, iResult) % ===== CHECK TIME ===== % If there time in this file if (length(Mat.Time) >= 2) - isTimeOkDs = 1; - % Save measures information if no DataFile is available - if isempty(GlobalData.DataSet(iDS).Measures) || isempty(GlobalData.DataSet(iDS).Measures.Time) - GlobalData.DataSet(iDS).Measures.Time = double(Mat.Time([1, end])); - GlobalData.DataSet(iDS).Measures.SamplingRate = double(Mat.Time(2) - Mat.Time(1)); - GlobalData.DataSet(iDS).Measures.NumberOfSamples = length(Mat.Time); - elseif (abs(Mat.Time(1) - GlobalData.DataSet(iDS).Measures.Time(1)) > 1e-5) || ... - (abs(Mat.Time(end) - GlobalData.DataSet(iDS).Measures.Time(2)) > 1e-5) || ... - ~isequal(length(Mat.Time), GlobalData.DataSet(iDS).Measures.NumberOfSamples) - isTimeOkDs = 0; - end % Update time window isTimeCoherent = CheckTimeWindows(); % If loaded file are not coherent with previous data if ~isTimeCoherent || ~isTimeOkDs - iDS = []; - bst_error(['Time definition for this file is not compatible with the other files' 10 ... - 'already loaded in Brainstorm.' 10 10 ... - 'Close existing windows before opening this file, or use the Navigator.'], 'Load matrix', 0); - return + res = java_dialog('question', [... + 'The time definition is not compatible with previously loaded files.' 10 ... + 'Unload all the other files first?' 10 10], 'Load matrix', [], {'Unload other files', 'Cancel'}); + % Cancel: Unload the new dataset + if isempty(res) || strcmpi(res, 'Cancel') + UnloadDataSets(iDS); + iDS = []; + return; + % Otherwise: unload all the other datasets + else + iDS = UnloadOtherDs(iDS); + if isempty(iDS) + iMatrix = []; + return + end + % Update time window + isTimeCoherent = CheckTimeWindows(); + end end % Update TimeWindow panel panel_time('UpdatePanel'); @@ -3556,5 +3576,24 @@ function SaveChannelFile(iDS) end end +%% ===== UNLOAD OTHER DS ===== +function iDS = UnloadOtherDs(iDS) +% Unload Brainstorm datasets except for iDS. It returns the new iDS (iDS=1) for the kept DS + global GlobalData; + % Save dataset to keep + bakDS = GlobalData.DataSet(iDS); + % Unload everything + UnloadAll('Forced'); + % If not everything was unloaded correctly (eg. the user cancelled half way when asked to save the modifications) + if ~isempty(GlobalData.DataSet) + % Unload also dataset to keep + UnloadDataSets(iDS); + iDS = []; + return; + end + % Restore dataset + GlobalData.DataSet = bakDS; + iDS = 1; +end diff --git a/toolbox/gui/view_surface_data.m b/toolbox/gui/view_surface_data.m index 46d98dd6c..bcfdbfcf1 100644 --- a/toolbox/gui/view_surface_data.m +++ b/toolbox/gui/view_surface_data.m @@ -167,6 +167,9 @@ iDS = bst_memory('GetDataSetSubject', sSubject.FileName, 1); end +if isempty(iDS) + return; +end %% ===== MODALITY ===== if isempty(Modality)