How Do You Correctly Parse and Convert Strings to Numbers in Octave?

In summary, the conversation discusses a problem with a script that reads data from a .txt file and stores it in two variables. The first variable works correctly, but the second one returns NaN values. Suggestions for troubleshooting include checking for extra characters and making sure the script is correctly interpreting the data. A simpler alternative script is also provided.
  • #1
dRic2
Gold Member
883
225
Hi, I used to use MATLAB for this kind of thing, but now my pc broke and I need to run some scripts. I have a .txt file structured like this

10 -2.34454
12 -2.34566
14 -2.34677
... ...

and I want to store the data in two variables: the first is the "counting" (10, 12, ...) and the second is the value (-2.34454, ...). So I wrote the following script, but I run in the following problem

Code:
function [out1, out2] = read(fid)
    i = 1;

    while true
      currentline = fgetl(fid);
      if (currentline == -1)
        break;
      end
 
      pos = find(currentline == ' ');
 
      for j=1:pos
        count(j) = currentline(j);
      end

      for j=(pos+1):(length(currentline))
        value(j) = currentline(j);
      end
   
      count_num(i) = str2num(count);   % <--------- THIS WORKS
      value_num(i) = str2double(value); % <-------- THIS DOES NOT WORK !
      i++;
    end
 
    out1 = count_num;
    out2 = value_num;
end

The problem is that while I get my nice vector count_num = [10 12 14 ...], the other vector is [NaN, NaN, ...]. I also tried to add disp(value) to see if the string is acquired correctly and it is! The problem is the conversion! str2num also doesn't work.

Any help would be highly appreciated.

Ric
 
Physics news on Phys.org
  • #2
The first thing I would do is check if your string starts with a space and you need to just exclude it. The second thing is if it's interpreting the - as a minus sign correctly - if you omit that character does it work? Lastly I would try to drop the last character, it might be reading a weird ending character it doesn't know what to do with.
 
  • Like
Likes dRic2 and Dale
  • #3
Line 17 should probably be value(j-pos) or something similar.

In general, when you have a standard routine failing you need to look at the inputs.
 
  • Love
Likes dRic2
  • #4
Dale said:
Line 17 should probably be value(j-pos)
Thank you so much
 
  • Like
Likes Dale
  • #5
This script can be massively simplified:

read text file:
function [out1, out2] = read(fileName)

    fid = fopen(fileName)
    d = fscanf(fid,'%f', [2 inf])';
    out1 = d(:,1);
    out2 = d(:,2);
    fclose(fid);end
 
  • Like
Likes dRic2
  • #6
Thanks @Arjan82, I'll look for the documentation about fscanf :)
 

1. How do I read numbers from a file using Octave?

To read numbers from a file using Octave, you can use the fscanf function. This function reads formatted data from a file and stores it in a variable. You will need to specify the file name, format of the data, and the variable where you want to store the data.

2. What is the syntax for using the fscanf function in Octave?

The syntax for using the fscanf function in Octave is as follows: variable = fscanf(fileID, format, size). Here, fileID is the file identifier, format is the format of the data, and size is the size of the data to be read.

3. Can I read numbers of different data types from a file using Octave?

Yes, you can read numbers of different data types from a file using Octave. The fscanf function allows you to specify the format for each data type. For example, if you want to read a decimal number, you can use %f in the format string.

4. How do I handle errors while reading numbers from a file in Octave?

To handle errors while reading numbers from a file in Octave, you can use the error function. This function allows you to display an error message and stop the execution of your code if an error occurs. You can also use try-catch statements to catch and handle specific errors.

5. Can I read numbers from a specific line in a file using Octave?

Yes, you can read numbers from a specific line in a file using Octave. You can use the fseek function to move the file pointer to a specific line before using the fscanf function to read the numbers. You will need to know the size of each line in the file or use the ftell function to get the current position of the file pointer.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
23
Views
7K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
6K
  • Programming and Computer Science
Replies
8
Views
3K
  • Programming and Computer Science
Replies
4
Views
11K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
4
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
0
Views
2K
Back
Top