MATLAB extract numbers in .txt file

In summary, the conversation discusses a question about extracting information from a .txt file with a combination of letters and numbers. The file contains two sets of numbers, representing coordinate pairs. The solution involves using the import data function in MATLAB.
  • #1
member 428835
Hi PF!

I have a .txt file with spaces and letters, along with numbers. It looks something like this:

12.0 42.0 21.0 32.0
ICF-8-1
23.0 24.0 46.0 600.0

Additionally, the first two sets of numbers each represent a coordinate pair. Then the first coordinate is (12,42) and the second is (21,32) and so on. Does anyone know how I can have MATLAB extract this info? I've attached the .txt file so you can see what I'm looking at.

Thanks so much!
 

Attachments

  • ResultsICF8-3-T111.txt
    10.2 KB · Views: 437
Physics news on Phys.org
  • #2
joshmccraney said:
Hi PF!

I have a .txt file with spaces and letters, along with numbers. It looks something like this:

12.0 42.0 21.0 32.0
ICF-8-1
23.0 24.0 46.0 600.0

Additionally, the first two sets of numbers each represent a coordinate pair. Then the first coordinate is (12,42) and the second is (21,32) and so on. Does anyone know how I can have MATLAB extract this info? I've attached the .txt file so you can see what I'm looking at.

Thanks so much!

Did you try just using import data? It can sometimes parse things out nicely.
 
  • #3
joshmccraney said:
Hi PF!

I have a .txt file with spaces and letters, along with numbers. It looks something like this:

12.0 42.0 21.0 32.0
ICF-8-1
23.0 24.0 46.0 600.0

Additionally, the first two sets of numbers each represent a coordinate pair. Then the first coordinate is (12,42) and the second is (21,32) and so on. Does anyone know how I can have MATLAB extract this info? I've attached the .txt file so you can see what I'm looking at.

Thanks so much!

Also depends on your version, newer versions have a nicer import data app. I got it to work in mine, but I had to delete the top characters manually.
 
  • Like
Likes member 428835
  • #4
Thanks Student100! This totally worked!
 

Related to MATLAB extract numbers in .txt file

Question 1: What is MATLAB?

MATLAB is a programming language and interactive environment used by scientists and engineers for numerical computation, data analysis, and visualization.

Question 2: How can I extract numbers from a .txt file using MATLAB?

To extract numbers from a .txt file in MATLAB, you can use the textscan function. This function reads data from a text file and stores it in a cell array, which can then be converted to a numerical array using the cell2mat function.

Question 3: Can I extract specific numbers from a .txt file using MATLAB?

Yes, you can specify the format and location of the numbers you want to extract using the textscan function. This allows you to extract only the numbers that meet certain criteria from the file.

Question 4: What is the difference between textscan and importdata for extracting numbers from a .txt file in MATLAB?

The textscan function is more versatile and allows for more control over the extraction process, while importdata is a simpler function that automatically imports all data from a file into a numerical array. However, importdata may not work for files with non-uniform data or headers.

Question 5: Can I save the extracted numbers from a .txt file in MATLAB to a new file?

Yes, you can use the fprintf function to write the extracted numbers to a new .txt file in a specific format. This allows you to save the data for future use or analysis.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
24K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
  • General Discussion
Replies
19
Views
2K
Back
Top