function [new_header] = setparameter(header, parameter, value, index) %SETPARAMETER Set the value of a specified header parameter. % % NEW_HEADER = SETPARAMETER(HEADER, PARAMETER, VALUE, INDEX) % % PARAMETERS: 1) The header in which to set the new parameter value. % 2) The parameter name as a string. % 3) The value to set. % 4) The index of the parameter to be set. If not % defined, all the parameters will be changed. % % If the parameter does not exist, it will be appended to % the header. % % RETURN: On success, return the modified header else return % an empty matrix. % % Claudio 6 May 1994. Modified 7 April 1995 by Mark to fix a problem % when two parameters have the same value % % Copyright (c) 1995 by Claudio Rivetti and Mark Young % claudio@alice.uoregon.edu, mark@alice.uoregon.edu % if nargin<3 error('Too few input arguments'); end if nargin>4 error('Too many input arguments'); end header = char(header); EOF = char(26); AlexNEWLINE = char(13); new_header = []; sep=''; i=1; if isempty(header) return; end if nargin==3 index=0; end hlen=length(header); str_value=[]; if ~isstr(value) for i=1:length(value) str_value=[str_value, num2str(value(i)), ' ']; end else str_value=value; end if ~isempty(str_value) str_value = deblank(str_value); str_value = deblank(fliplr(str_value)); str_value = fliplr(str_value); sep=': '; end parameter = strrep(parameter, ':', ''); parameter = strrep(parameter, '\', ''); parameter = deblank(parameter); parameter = deblank(fliplr(parameter)); parameter = fliplr(parameter); cs=findstr(header, ': '); nl=find(header == AlexNEWLINE); p1=findstr(upper(header), upper(['\' parameter ':'])); parameter(1)=upper(parameter(1)); % if the parameter does not exist it will be added % at the end of the Header. if isempty(p1) parval=['\', parameter, sep, str_value, AlexNEWLINE 10 AlexEOF]; new_header=strrep(header, AlexEOF, parval); else for i=1:length(p1) p2=[p2 min(find(cs>p1(i)))]; p3=[p3 min(find(nl>p1(i)))]; end p2=cs(p2); p3=nl(p3)-1; str_value = ['\' parameter sep str_value]; if index new_header=strrep(header, header(p1(index):p3(index)), str_value); else new_header=header; for i=1:length(p1) new_header=strrep(new_header, header(p1(i):p3(i)), str_value); end end end % Truncate or add zeros at the end of the header in order to keep % the same header size. newhlen=length(new_header); if newhlen > hlen new_header=new_header(1:hlen); else new_header=[new_header zeros(1,hlen-newhlen)]; end return;