Skip to content

Commit

Permalink
Bugfix: Handle better wrong time and frequency bands format errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ftadel committed Sep 17, 2021
1 parent 38cfd75 commit 0ac9259
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
2 changes: 1 addition & 1 deletion toolbox/core/bst_get.m
Expand Up @@ -3130,7 +3130,7 @@
if isempty(argout1.Freqs)
argout1.Freqs = defPref.Freqs;
end
if ~isempty(argout1.FreqBands) && ~ischar(argout1.FreqBands{1,2})
if ~isempty(argout1.FreqBands) && ((size(argout1.FreqBands,2) ~= 3) || ~all(cellfun(@ischar, argout1.FreqBands(:))) || any(cellfun(@(c)isempty(strtrim(c)), argout1.FreqBands(:))))
argout1.FreqBands = defPref.FreqBands;
end

Expand Down
13 changes: 12 additions & 1 deletion toolbox/process/functions/process_tf_bands.m
Expand Up @@ -25,7 +25,7 @@
% For more information type "brainstorm license" at command prompt.
% =============================================================================@
%
% Authors: Francois Tadel, 2012-2014
% Authors: Francois Tadel, 2012-2021

eval(macro_method);
end
Expand Down Expand Up @@ -153,6 +153,12 @@

% ===== FREQUENCY BANDS =====
if ~isempty(FreqBands)
% Check format
if (size(FreqBands,2) ~= 3) || ~all(cellfun(@ischar, FreqBands(:))) || any(cellfun(@(c)isempty(strtrim(c)), FreqBands(:)))
Messages = 'Invalid frequency band format.';
TimefreqMat = [];
return;
end
% Frequency bounds
BandBounds = GetBounds(FreqBands);
% Number of bands
Expand Down Expand Up @@ -258,6 +264,11 @@
for iBand = 1:length(lineBand)
% Split line
valBand = str_split(lineBand{iBand}, '/\|');
if (length(valBand) ~= 3) || any(cellfun(@(c)isempty(strtrim(c)), valBand))
disp(['BST> Error: Invalid time or frequency band "' lineBand{iBand} '".']);
Bands = {};
return;
end
for i = 1:length(valBand)
Bands{iBand,i} = strtrim(valBand{i});
end
Expand Down
13 changes: 10 additions & 3 deletions toolbox/timefreq/bst_timefreq.m
Expand Up @@ -498,7 +498,7 @@
end
end
% Invalid frequencies
if any(OPTIONS.Freqs <= 0)
if iscell(OPTIONS.Freqs) || isempty(OPTIONS.Freqs) || any(OPTIONS.Freqs <= 0)
Messages = 'Invalid frequency definition: All frequencies must be > 0.';
isError = 1;
return;
Expand Down Expand Up @@ -825,9 +825,16 @@ function SaveFile(iTargetStudy, DataFile, DataType, RowNames, TF, OPTIONS, FreqB
% Apply time and frequency bands
if ~isempty(FreqBands) || ~isempty(OPTIONS.TimeBands)
if strcmpi(OPTIONS.Method, 'hilbert') && ~isempty(OPTIONS.TimeBands)
FileMat = process_tf_bands('Compute', FileMat, [], OPTIONS.TimeBands);
[FileMat, Messages] = process_tf_bands('Compute', FileMat, [], OPTIONS.TimeBands);
elseif strcmpi(OPTIONS.Method, 'morlet') || strcmpi(OPTIONS.Method, 'psd')
FileMat = process_tf_bands('Compute', FileMat, FreqBands, OPTIONS.TimeBands);
[FileMat, Messages] = process_tf_bands('Compute', FileMat, FreqBands, OPTIONS.TimeBands);
end
if isempty(FileMat)
if ~isempty(Messages)
error(Messages);
else
error('Unknow error while processing time or frequency bands.');
end
end
end

Expand Down

0 comments on commit 0ac9259

Please sign in to comment.