Retrieve data from a .txt file in Scilab
Flo
-
janeo -
janeo -
Hello,
I'm starting out with Scilab and I've never used Matlab (not very advantageous!)
My goal is to process a ‘.txt’ file in Scilab to extract data from it.
Typically, my file consists of a header with 83 lines (which I manage to skip) and 4096 lines of measurement results, written in the following format:
I aim to retrieve the real part into a 1-row by 4096-column array.
I have therefore written the following program in Scilab:
And when I execute it, I get:
The function ‘msscanf’ does allow me to get the desired line as a character string, but when I try to convert it to numbers using the ‘evstr’ function, it turns into nonsense!
Is this due to my variable types ‘%s’? (I tried with ‘%f’ and it’s even worse). Or does it come from the type of conversion I’m using? I know that under Matlab there is a ‘textscan’ function that can do what I want, but I don’t know of any equivalent in Scilab.
I'm currently testing with the ‘read’ function and another way to proceed, but it still doesn’t work and returns the same values.
Thank you for your help and please detail your response as I’m not great with Scilab.
Best regards.
I'm starting out with Scilab and I've never used Matlab (not very advantageous!)
My goal is to process a ‘.txt’ file in Scilab to extract data from it.
Typically, my file consists of a header with 83 lines (which I manage to skip) and 4096 lines of measurement results, written in the following format:
Measurement No. Time Real Part Imaginary Part 1 -4.0000000000e-002 1.27177e-002 9.86312e-004
I aim to retrieve the real part into a 1-row by 4096-column array.
I have therefore written the following program in Scilab:
// Opening the file file_mes_lib=mopen('File name','r'); // Reading the lines of the given measurement donnee_mesure_char_lib=mgetl(file_mes_lib,4096); // Closing the file mclose(file_mes_lib); // Converting character string data to numerical values donnee_mesure_lib_char=msscanf(donnee_mesure_char_lib,'%s %s %s %s'); donnee_mesure_lib=evstr(donnee_mesure_lib_char); // Retrieving the data and reshaping donnee_mesure_libre=donnee_mesure_lib(:,3)'; And when I execute it, I get:
donnee_mesure_lib_char = !1 -4.0000000000e-002 1.27177e-002 9.86312e-004 ! donnee_mesure_lib = 1. - 4. 0. 1. 271.77 9. 8.6312 donnee_mesure_libre = 0.
The function ‘msscanf’ does allow me to get the desired line as a character string, but when I try to convert it to numbers using the ‘evstr’ function, it turns into nonsense!
Is this due to my variable types ‘%s’? (I tried with ‘%f’ and it’s even worse). Or does it come from the type of conversion I’m using? I know that under Matlab there is a ‘textscan’ function that can do what I want, but I don’t know of any equivalent in Scilab.
I'm currently testing with the ‘read’ function and another way to proceed, but it still doesn’t work and returns the same values.
Thank you for your help and please detail your response as I’m not great with Scilab.
Best regards.
Configuration: Windows XP Internet Explorer 6.0
1 answer
-
Hello Flo,
The problem comes from the commas; in Scilab, the comma separates two entries of a matrix on the same line. We also have the same effect with a space, for example:
-->[1, 2, 3; 4 5 6]
ans =
1. 2. 3.
4. 5. 6.
The "decimal" separator is actually the point, so you need to replace the commas in your file with points. You have two options: either you do it with an editor before loading your data into Scilab, or you do it after the mgetl using the strsubst function:
-->M=['1 2,3 4,567e8 9e-10']//one line with 4 entries
M =
1 2,3 4,567e8 9e-10
-->B=strsubst(M,',','.')//we change , to . in M
B =
1 2.3 4.567e8 9e-10
-->evstr(B)//we interpret the string
ans =
1. 2.3 4.567D+08 9.000D-10
Philippe.-
Thank you very much, Philippe. My problem indeed came from the commas. I realized this after comparing with a colleague's research. I made sure that the software that outputs my '.txt' file puts periods instead of commas and now it works well! But I have no doubt that your method works too ;)
Thanks again
See you soon
-