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

Click For Summary

Discussion Overview

The discussion focuses on parsing and converting strings to numbers in Octave, particularly in the context of reading data from a text file structured with two columns of numerical values. Participants explore issues related to string manipulation and data conversion within a custom function.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant describes a script that successfully converts one column of data but fails to convert the second column, resulting in NaN values.
  • Another participant suggests checking if the string starts with a space, if the minus sign is interpreted correctly, and if there are any unexpected ending characters.
  • One participant points out a potential issue with indexing in the script, suggesting that the variable assignment for the value should be adjusted to account for the position of the space.
  • A different participant proposes a simplified version of the script using fscanf for reading the data, which could streamline the process.

Areas of Agreement / Disagreement

Participants express differing views on the best approach to resolve the issue, with some suggesting specific debugging steps while others propose a complete rewrite of the function. No consensus is reached on a single solution.

Contextual Notes

There are unresolved assumptions regarding the format of the input data and the handling of string manipulation in Octave. The discussion does not clarify whether all potential edge cases have been considered.

dRic2
Gold Member
Messages
887
Reaction score
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
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   Reactions: dRic2 and Dale
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   Reactions: dRic2
Dale said:
Line 17 should probably be value(j-pos)
Thank you so much
 
  • Like
Likes   Reactions: Dale
This script can be massively simplified:

[CODE lang="matlab" title="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
[/CODE]
 
  • Like
Likes   Reactions: dRic2
Thanks @Arjan82, I'll look for the documentation about fscanf :)
 

Similar threads

Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 23 ·
Replies
23
Views
9K
  • · Replies 4 ·
Replies
4
Views
12K
  • · Replies 8 ·
Replies
8
Views
5K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 6 ·
Replies
6
Views
2K
Replies
1
Views
2K