]> Creatis software - CreaPhase.git/blob - octave_packages/dataframe-0.9.1/@dataframe/private/df_strset.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / dataframe-0.9.1 / @dataframe / private / df_strset.m
1 function [x, over] = df_strset(x, over, S, RHS, pad = ' ')
2   %# x = df_strset(x, over, S, RHS, pad = " ")
3   %# replaces the strings in cellstr x at indr by strings at y. Adapt
4   %# the width of x if required. Use x 'over' attribute to display a
5   %# message in case strings are overwritten.
6
7   %% Copyright (C) 2009-2012 Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
8   %%
9   %% This file is part of Octave.
10   %%
11   %% Octave is free software; you can redistribute it and/or
12   %% modify it under the terms of the GNU General Public
13   %% License as published by the Free Software Foundation;
14   %% either version 2, or (at your option) any later version.
15   %%
16   %% Octave is distributed in the hope that it will be useful,
17   %% but WITHOUT ANY WARRANTY; without even the implied
18   %% warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
19   %% PURPOSE.  See the GNU General Public License for more
20   %% details.
21   %%
22   %% You should have received a copy of the GNU General Public
23   %% License along with Octave; see the file COPYING.  If not,
24   %% write to the Free Software Foundation, 51 Franklin Street -
25   %% Fifth Floor, Boston, MA 02110-1301, USA.
26   
27   %#
28   %# $Id: df_strset.m 9585 2012-02-05 15:32:46Z cdemills $
29   %#
30
31   %# adjust x size, if required
32   if isnull(RHS),
33     %# clearing
34     if isempty(S),
35       x = cell(0, 1); over = zeros(1, 0);
36       return
37     endif
38     dummy = S; dummy(1).subs(2:end) = [];
39     over = builtin('subsasgn', over, dummy, true);
40   else
41     if isempty(S), %# complete overwrite
42       if ischar(RHS), RHS = cellstr(RHS); endif
43       nrow = length(RHS);
44       if any(~over(nrow)),
45         warning('going to overwrite names');
46       endif
47       x(1:nrow) = RHS;
48       over(1:nrow) = false;
49       if nrow < length(x),
50         x(nrow+1:end) = {pad};
51       endif
52       return
53     else
54       dummy = S(1); dummy.subs(2:end) = []; % keep first dim only
55       if any(~(builtin('subsref', over, dummy)));
56         warning('going to overwrite names');
57       endif
58       over = builtin('subsasgn', over, dummy, false);
59     endif
60   endif
61
62   %# common part
63   if ischar(RHS) && length(S(1).subs) > 1, 
64     %# partial accesses to a char array
65     dummy = char(x);
66     dummy = builtin('subsasgn', dummy, S, RHS);
67     if isempty(dummy),
68       x = cell(0, 1); over = zeros(1, 0);
69       return
70     endif
71     if size(dummy, 1) == length(x),
72       x = cellstr(dummy);
73       return
74     endif
75     %# partial clearing gone wrong ? retry
76     RHS = { RHS }; 
77   endif
78   x = builtin('subsasgn', x, S, RHS);
79     
80 endfunction