[Scilab-users] Reading numerical and string vectors from txt-file
Jens Simon Strom
j.s.strom at hslmg.de
Fri Mar 24 16:24:52 CET 2017
mgetl(filename) is a good alternative. I have used it in a function
because retrieving numbers comes up frequently for me.
function cv=!cv(fp,colspan,varargin)//Extracts a numeric Colum Vector from ASCII file
//Data separator in file: spaces only, number may differ from line to line to cope with ragged colums
//fp: full path of ASCII file, type string
//colspan: Increasing row vector of colum indices, position range of the numbers, type constant
// The data colum may be ragged but no other characters may protrude into the colum span.
//varargin: optional string vector, line numbers to read
//cv: colum vector of read numbers, type constant
//CAVEAT: If the file uses separating tabs they should be replaced by blanks. colspan should be enclosed
// in at least one leading and one trailing blank in all lines (check in editor).
//Examples:
//cv=!cv('file.txt',[12:15]) //reads colums 12 to 15 of all lines
//cv=!cv('file.txt',[1:6],'[10:$-3]') //reads colums 1 to 6 of 10th to third last line, good for masking headers and footers
//cv=!cv('file.txt',[1:6],'[1 11 101]') //reads distant lines
fdescr=mopen(fp,'r');//file descriptor
txtlines = mgetl(fdescr,-1);//string colum vector with all lines
mclose(fdescr);
[lhs,rhs]=argn(0)
if rhs==3 then
execstr('txtlines=txtlines('+varargin(1)+')')//reduction to the requested lines
end
cv = strtod(part(txtlines,colspan))//string to double
endfunction
Jens
---------------------------------------------------------------------------------------------------
Am 23.03.2017 09:51, schrieb Jan Åge Langeland:
>
>
>
> On 23.03.2017 00:25, Jens Simon Strom wrote:
>> The test data I posted had been preprocessed manually by equalizing
>> the spaces between the colums to a single one. The original output of
>> the MICA astro software aligns the colums tidily so that the number
>> of spaces varies along a line and from line to line if the values
>> vary between 1 and 3 digits. One can fix that with regex. It would be
>> workflow supporting if the separator input ofcsvRead would interpret
>> several blanks as equivalent to one or if automation could be
>> acchieved elsewise.
>> Jens
>> --------------------------------------------------------------------------------------------------------------------------------------------------
>>
> If you have fixed column widths, maybe something like this is better:
>
> Ma=mgetl(filename)
> skiplines=7
> clear Md
> for i=skiplines+1:size(Ma,1)
> Md(i-skiplines,1:22)=strsplit(Ma(i),[4 8 12 17 20 21 24 26 27 29 33 35
> 36 38 42 45 46 48 53 55 56])'
> end
>
> Jan-Åge
>
>
> _______________________________________________
> users mailing list
> users at lists.scilab.org
> http://lists.scilab.org/mailman/listinfo/users
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.scilab.org/pipermail/users/attachments/20170324/cc88a2b5/attachment.htm>
More information about the users
mailing list