Search code examples
scilab

Read hexadecimal from file with scilab


I have a file that looks like the following:

Time    M1:Address 480008C0
0   79F9446F
0.000125    7AE7446B
0.00025 7BA8446F
...

It is 2 numbers separated by a tab, one is a hexadecimal number. I am looking to read these into a decimal array in scilab. I have tried to open and read the file as follows:

u=mopen('proper_log_with_fail.txt','r'); // open the file for reading
s = mscanf(u, '%s'); // Forget the first line
[time,val]=mfscanf(u,'%e\t%x'); // Get the second line

So i am not ever able to read any values ...? I cannot even read the first string? Am i doing something super obvious wrong?


Solution

  • Maybe a typo, but for the first row you are using mscanf which should be mfscanf for reading from file. When changing this it shows that your first line isn't matching, note that %sonly matches one word and then stops at the next whitespace. To read (and discard) the first line, use mgetl and tell it to read 1 line. If the rest of the input file is data formatted the same way, you can use the niter argument of mfscanf to keep matching up until the end of the file (EOF).

    file_path = 'proper_log_with_fail.txt'; 
    
    // open the file for reading
    fd=mopen(file_path,'r'); 
    
    // Read/display header line
    number_of_lines_to_read = 1;
    header = mgetl(fd, number_of_lines_to_read);
    disp(header);
    
    // Scan all lines up until EOF 
    niter = -1
    s = mfscanf(niter, fd, '%e %x'); 
    disp(s)
    
    // Close the file after reading
    mclose(fd)