[Scilab-users] Read file (.txt)

Jan Åge Langeland j-lan at online.no
Thu Sep 10 06:39:17 CEST 2020


Hi Daniel

I made my own csvread, it seems to read your file OK with

exec('JcsvRead3.sce', -1);

M=JcsvRead3('output.txt','   ');

Brgds

Jan

On 2020-09-10 0:03 AM, Daniel Stringari wrote:
> Good evening everyone,
>
> I'm integrating scilab with other software, so I need to read an output file
> (.txt), like the one attached (output).
>
> It is worth mentioning that this file does not always have the same
> dimensions (matrix), so I am trying to find something robust that can deal
> with this particularity. I tried to use the 'csvread' function and the
> 'read' function, but I still haven't been successful ... Any suggestions?
>
> output.txt <http://mailinglists.scilab.org/file/t498028/output.txt>
>
>
>
>
>
> --
> Sent from: http://mailinglists.scilab.org/Scilab-users-Mailing-Lists-Archives-f2602246.html
> _______________________________________________
> 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/20200910/50f4d3cf/attachment.htm>
-------------- next part --------------
//Read CSV file -JÅ 2020
function M=JcsvRead3(filename,varargin)

    linestoread=-1;// -1 for all, argument 6
    headerlines=0;//argument 7
    footerlines=0;//argument 8
   
    separator=";"//default, can be changed by argument 2. Several separators accepted
    decimal="."; //default, can be changed to , by argument 3
    num_asc="double" // default , can be changed to "ascii" by argument 4

    if argn(2)>1 
        t1=type(varargin(1));
        if t1==10 then 
            separator=varargin(1);
        else
            mprintf("Default separator\n")
        end
    end
    if argn(2)>2 
        t2=type(varargin(2));
        if t2==10 && varargin(2)=="," then 
            decimal="," 
        elseif t2>0 && varargin(2)~="."
            mprintf("Decimal, select . or, \n");
            err=return(varargin(2)) ;
        end;
    end
    if argn(2)>3 
        t3=type(varargin(3));
        if  t3>0 then num_asc=varargin(3) end;
    end
    if argn(2)>6 
        t6=type(varargin(6));
        if  t6>0 && varargin(6)~=[] then linestoread=varargin(6) end;
    end
    if argn(2)>7 
        t7=type(varargin(7));
        if  t7>0 && varargin(7)~=[] then headerlines=varargin(7) end;
    end
    if argn(2)>8
        t8=type(varargin(8));
        if  t8==1 && varargin(8)~=[] then footerlines=varargin(8) end;
    end
    fid = mopen(filename,'rt');
    csvline=mgetl(fid,linestoread);
    mclose(fid);
    n=size(csvline,1);
    select num_asc;
    case "double" then
        M(1)=1;
        for k=headerlines+1:n-footerlines
            datatemp=strtod(strsplit(csvline(k),separator),decimal);
            M(k-headerlines,1:length(datatemp))=datatemp';
        end
    case "string" then
        M(1)=" "
        for k=headerlines+1:n-footerlines
            datatemp=strsplit(csvline(k),separator);
            datatemp2=datatemp
            M((k-headerlines),1:size(datatemp',2))=datatemp';
        end
    else
        mprintf("Conversion double or string\n")
        err=return(num_asc);
    end
endfunction



More information about the users mailing list