Skip to content

Commit

Permalink
Update string2indicator.m
Browse files Browse the repository at this point in the history
  • Loading branch information
torwager committed Jan 25, 2024
1 parent 7c32303 commit c77c20b
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions densityUtility3/Support_functions/string2indicator.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,33 @@
% Examples:
%[indic,nms] = string2indicator(CL{1}(1).valence);
%[indic,nms] = string2indicator(CL{1}(1).valence,{'neg' 'pos'});
%
% Note: This function changed 11/1/2023 to return names in stable order
% (the order which they appear in the original str input).
% Also, previously, the function would accept a non-cell string matrix but
% return potentially incorrect output; now enforces cell array


if ~iscell(str)
str = cellstr(str);
end

if length(varargin) > 0
if ~isempty(varargin)
nms = varargin{1};
else
nms = unique(str);
nms = unique(str, 'stable'); % tor edited 11/1/2023, 'rows' would fix edge case with non-cell string matrix, but now enforce cell anyway
end

if ~iscell(nms), for i = 1:length(nms), nms2{i} = nms(i);,end, nms = nms2;,end

nms(strcmp(nms,'NaN')) = [];
if ~iscell(nms) %, for i = 1:length(nms), nms2{i} = nms(i); end, nms = nms2; end % this was returning only the first character of a string matrix
nms = cellstr(nms);
end

nms(strcmp(nms, 'NaN')) = [];

indic = zeros(length(str),1);
indic = zeros(length(str), 1);

for k = 1:length(nms)
indic(:,k) = strcmp(str,nms{k});
indic(:,k) = strcmp(str, nms{k});
end

if size(nms,1) > size(nms,2)
Expand All @@ -33,4 +45,4 @@
condf = indic2condf(indic);
end

return
end

0 comments on commit c77c20b

Please sign in to comment.