{"id":1194,"date":"2015-09-26T05:44:21","date_gmt":"2015-09-26T10:44:21","guid":{"rendered":"http:\/\/www.amibroker.com\/kb\/?p=1194"},"modified":"2015-10-01T05:47:40","modified_gmt":"2015-10-01T10:47:40","slug":"how-to-populate-matrix-from-a-text-file","status":"publish","type":"post","link":"https:\/\/www.amibroker.com\/wordpress\/kb\/2015\/09\/26\/how-to-populate-matrix-from-a-text-file\/","title":{"rendered":"How to populate Matrix from a text file"},"content":{"rendered":"

AmiBroker 6.00 has introduced support for matrices. After we create a matrics with Matrix function call:<\/p>my_var_name <\/span>= <\/span>Matrix<\/span>( <\/span>rows<\/span>, <\/span>cols<\/span>, <\/span>initvalue<\/span>)<\/code>

then in order to access matrix elements, we need to use:
x <\/span>= <\/span>my_var_name<\/span>[ <\/span>row <\/span>][ <\/span>col <\/span>]<\/code>

However – if we want to populate a relatively large matrix with values generated in other programs, then it may not be very practical to do it by hand in the AFL code assigning individual elements like this:<\/p>A<\/span>[ <\/span>0 <\/span>][ <\/span>0 <\/span>] = <\/span>1<\/span>; <\/span>A<\/span>[ <\/span>0 <\/span>][ <\/span>1 <\/span>] = <\/span>4<\/span>; <\/span>A<\/span>[ <\/span>0 <\/span>][ <\/span>2 <\/span>] = <\/span>6<\/span><\/code>

What we can do in such case is to store the values in a text file that we could use as input, then read through the file using fgets<\/strong> function and populate Matrix elements using a looping code. A sample formula showing how to perform such task is presented below.<\/p>

A sample text file for this example can be found here: http:\/\/www.amibroker.com\/kb\/wp-content\/uploads\/2015\/10\/samplematrix.txt<\/a><\/p><\/span>\/\/ the input file path
<\/span>file <\/span>= <\/span>"C:\\\\samplematrix.txt"<\/span>;

<\/span>\/\/ define the size of the desired matrix
<\/span>rows <\/span>= <\/span>16<\/span>;
<\/span>cols <\/span>= <\/span>16<\/span>;

<\/span>\/\/ create matrix
<\/span>myMatrix <\/span>= <\/span>Matrix<\/span>( <\/span>rows<\/span>, <\/span>cols<\/span>, <\/span>0 <\/span>);

<\/span>\/\/ open file
<\/span>fh <\/span>= <\/span>fopen<\/span>( <\/span>file<\/span>, <\/span>"r" <\/span>);

if( <\/span>fh <\/span>)
{
    <\/span>i <\/span>= <\/span>0<\/span>;

    <\/span>\/\/ iterate through the lines of input file
    <\/span>for( <\/span>i <\/span>= <\/span>0<\/span>; ! <\/span>feof<\/span>( <\/span>fh <\/span>) AND <\/span>i <\/span>< <\/span>rows<\/span>; <\/span>i<\/span>++ ) 
    {
        <\/span>\/\/ read a line of text
        <\/span>line <\/span>= <\/span>fgets<\/span>( <\/span>fh <\/span>); 

        if( <\/span>line <\/span>== <\/span>"" <\/span>)
        {
            <\/span>Error<\/span>(<\/span>"Too few rows in the data file or an empty row found"<\/span>);
            break;
        }
    
        <\/span>\/\/ iterate through the elements of the line
        <\/span>for( <\/span>j <\/span>= <\/span>0<\/span>; ( <\/span>item <\/span>= <\/span>StrExtract<\/span>( <\/span>line<\/span>, <\/span>j <\/span>) ) != <\/span>"" <\/span>AND <\/span>j <\/span>< <\/span>cols<\/span>; <\/span>j<\/span>++ ) 
        {
            <\/span>\/\/ assign matrix element
            <\/span>myMatrix<\/span>[ <\/span>i <\/span>][ <\/span>j <\/span>] = <\/span>StrToNum<\/span>( <\/span>item <\/span>);
        }
        
        if( <\/span>j <\/span>< <\/span>cols <\/span>)
        {
            <\/span>Error<\/span>(<\/span>"Too few columns in data file"<\/span>);
            break;
        }
    }
    
    <\/span>fclose<\/span>( <\/span>fh <\/span>);
}
else
{
    <\/span>Error<\/span>( <\/span>"ERROR: file can not be opened" <\/span>);
}

<\/span>\/\/ spot check selected element
<\/span>Title <\/span>= <\/span>"spot check M[ 2 ][ 3 ]: " <\/span>+ <\/span>NumToStr<\/span>( <\/span>MyMatrix<\/span>[ <\/span>2 <\/span>][ <\/span>3 <\/span>] )<\/code>","protected":false},"excerpt":{"rendered":"

AmiBroker 6.00 has introduced support for matrices. After we create a matrics with Matrix function call:my_var_name = Matrix( rows, cols, initvalue)then in order to access matrix elements, we need to use:x = my_var_name[ row ][ col ]However – if we want to populate a relatively large matrix with values generated in other programs, then it may not be very practical to do it by hand in […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[53,66,65],"_links":{"self":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/1194"}],"collection":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/comments?post=1194"}],"version-history":[{"count":2,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/1194\/revisions"}],"predecessor-version":[{"id":1199,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/1194\/revisions\/1199"}],"wp:attachment":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/media?parent=1194"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/categories?post=1194"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/tags?post=1194"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}