Reading double Numbers from a text file which contains string and number mixed

textscan
matlab read text file line by line
matlab textscan line by line
matlab read numbers from text file
read text file - matlab
read only numbers from text file python
matlab read text file into matrix
how to scan a text file in matlab

I have a file which contains Numbers and Texts. and I'm trying to read all numbers as double and put them in a one dimension double array.

In the file , some lines begin with Space. also some lines contain Two or Three numbers after each other. The file is creating from another app which i don't want to change its output format.

The data in the file is like blow and some lines begin with some space :

 110        ! R1
123.000753   ! Radian per s as R2
  600.0451  65     ! j/kg
 12000         ! 4 Number of iteration
 87.619    ! (min 20 and max 1000)

My code so far is :

 char[] splits = { ' ', '!' };
 var array = File.ReadAllLines(@"myfile.dat")
.SelectMany(linee => linee.Split(splits))
.Where(n => !string.IsNullOrWhiteSpace(n.ToString()))
.Select(n =>
 {
  double doub;
  bool suc = double.TryParse(n, out  doub);
  return new { doub, suc };
 }).Where( values=>values.suc).ToArray();

The problem is that my code also read numbers after ! in the descriptions like line 4 and line 5.

Array have to be like this :

110 , 123.000735 , 6000.0451 , 65 , 120000 , 87.619

But in my code is like this :

110 , 123.000735 , 6000.0451 , 65 , 120000 , 4 , 87.619 , 20 , 1000


It's hard to give a general formula when given only a single example, but the following will work for your example:

return File.ReadLines(@"myfile.dat")
    .Where(s => !String.IsNullOrWhiteSpace(s))
    .Select(s => s.Substring(0, s.IndexOf('!')).Split(new [] {' '}, StringSplitOptions.RemoveEmptyEntries))
    .SelectMany(s => s)
    .Select(s => Double.Parse(s));

[PDF] Reading data from a file Example: reading a file with numbers (part , We can read text in the file into strings in a program. This is a Problem: number is a string! INF1100 For many applications the integer index is "unnatural" - a general text (or Very often, a string contains lots of text and we want to split the. I want to read a text file having strings and Learn more about two line elements, tle, satellite, ephemeris, textscan, text file, read data Text Analytics Toolbox


One approach could be as following.

var lines = str.Split(new []{"!",Environment.NewLine},StringSplitOptions.RemoveEmptyEntries)
               .Where(x=> x.Split(new []{" "},StringSplitOptions.RemoveEmptyEntries).All(c=>double.TryParse(c, out _))).
               SelectMany(x=> x.Split(new []{" "},StringSplitOptions.RemoveEmptyEntries).Select(c=>double.Parse(c)));

[Java] Reading text files with mixture of strings and ints., How can you read an int when there is a mixture of strings and ints in a file. For example if there is a file like this: This is a file that contains exactly 9 words! I just need help understanding how to read a file when there are strings and ints mixed not the index (and it's using doubles exclusively, not one int and one double). Import Mixed Data from Text File into Table Open Live Script This example shows how to use the readtable function to import mixed text and numeric data into a table, specify the data types for the variables, and then append a new variable to the table.


Here's an alternate solution using regular expressions:

var regex = new Regex(@"^(\s*(?<v>\d+(\.\d+)?)\s*)+\!.*$");

var query = from line in lines
            let match = regex.Match(line)
            where match.Success
            from @group in match.Groups.Cast<Group>()
            where @group.Name == "v"
            select double.Parse(@group.Value, NumberStyles.Float, CultureInfo.InvariantCulture);

I want to read a text file having strings and numeric data. Is there any , I want to read a text file having strings and numeric data. directly import the lines identified with 2 (first col) as doubles but I will have to convert. When you have that mix of characters and numbers, there is no one-liner that  Reading numbers from general text-file. Learn more about reading, textfile, string, number, regexp, fscanf MATLAB


Read formatted data from text file or string, This MATLAB function reads data from an open text file into a cell array, C. Read a character vector containing floating-point numbers. formatSpec tells textscan to match each field in chr to a double-precision floating-point number. Import Block of Mixed Data from Text File into Table or Cell Array · Access Data in Cell  I need help with a sorting problem. A column holds numbers and numbers with suffixes. The conventional sort function pushes the text values to the bottom. I need them sorted along with the numbers.


Reading data from file, Reading a file into a string; Reading a mixture of text and numbers; Is it more to file reading mean = 0 for line in lines: number = float(line) mean = mean + number mean = mean/len(lines) Many data files contain a mix of text and numbers. but I am getting the same as it is there in the file. I want to read as such every text & number as it is. Please help me how can simply read as it is.


Reading a mixed Text and decimal file into an array, I am trying to read the following text file into an array containing the text #​include <stdio.h> #include <string.h> int main() { FILE *fpin0, *fpout; int c, i; int count=0; Your file contains one string and one double value per line. From a text string having more that one set of numbers - see example below: Brad James Company - Check - 23897 / invoice # 456755 issued Sept 1. Any suggestions are greatly appreciated - the line above is a sample of the excel items and after the check number shown there a number of invoice numbers in the text string. Thanks , Fred