Cameron asked . 2022-06-17

Extracting numerical data from text file

#Terrain File
Terrain.mat

#Start Point (xs,ys)
-4.8   4.0

From the code above I am trying to extract "-4.8" and save it as xs, and "4.0" saved as ys. I don't know how to tell matlab to look for the "#" that will be at the beginning of every category.

this is the project text file code please convert to project.text file for solving.

#This is a project file for MAE8 Spring 2014
 
#Terrain File
Terrain.mat
 
#Start Point (xs,ys)
-4.8   4.0
 
#End Point (xe,ye)
4.8   -4.8

#Number of Way Points
2
 
#Way Points (xwp,ywp)
-0.5 4.8
4.7 0.8

 

data import , AI, Data Science, and Statistics , Text Analytics Toolbox , Text Data Preparation

Expert Answer

Prashant Kumar answered . 2024-03-23 09:48:19

If all you need is that start point coordinates, then about the simplest is just

 

>> [xs ys]=textread('project.txt','%f %f', 1, ...
                    'headerlines',4,'commentstyle','shell');
>> disp([xs ys])
 -4.8000    4.0000

If, otoh, you need the other info as well, then it's essentially the same presuming you'll do a little post-processing--

>> [xs ys]=textread('project.txt','%f %f', ...
                    'headerlines',4,'commentstyle','shell');
>> disp([xs ys])
 -4.8000    4.0000
  4.8000   -4.8000
  2.0000         0
 -0.5000    4.8000
  4.7000    0.8000
>>
The only difference in the textread is the removal of the count of 1 to limit the first past to the one record; it instead iterates over the entire file from the 7th line on.
 
Note the #waypoints record is returned as [2 0]; hence one can use
 
ix=find(ys(:,2)==0};
as the locator for that record; get the number from the first column and then process the remaining records as the actual points.
 
ADDENDUM Actually, it dawned on me you don't need to search for the number of waypoints; it'll always (at least appears will be from this example file) the third row, so
 
np=xs(3);
*END*
You can get more complicated w/ textscan and/or scan thru on a record basis to find the sections, but sometimes the older, simpler ways are, well, just simpler...
 
Now, if you change your mind and also want the text line of the .mat file name, then textread isn't your friend as can't mix metaphors therein (so to speak, it can't mix the string/character data and the numeric). Then your options revert to textscan or the old standby fgetl. It doesn't look like these would ever be very large files so reading and processing the header and section heading lines on a record-by-record basis wouldn't be bad from a performance standpoint. Once you get to the waypoints section you can read the array w/ fscanf directly. This way has the advantage that you can make variable names for the sectional variables rather than using the row indices, but it's pretty much immaterial as to how from a practical standpoint.
 


Not satisfied with the answer ?? ASK NOW

Frequently Asked Questions

MATLAB offers tools for real-time AI applications, including Simulink for modeling and simulation. It can be used for developing algorithms and control systems for autonomous vehicles, robots, and other real-time AI systems.

MATLAB Online™ provides access to MATLAB® from your web browser. With MATLAB Online, your files are stored on MATLAB Drive™ and are available wherever you go. MATLAB Drive Connector synchronizes your files between your computers and MATLAB Online, providing offline access and eliminating the need to manually upload or download files. You can also run your files from the convenience of your smartphone or tablet by connecting to MathWorks® Cloud through the MATLAB Mobile™ app.

Yes, MATLAB provides tools and frameworks for deep learning, including the Deep Learning Toolbox. You can use MATLAB for tasks like building and training neural networks, image classification, and natural language processing.

MATLAB and Python are both popular choices for AI development. MATLAB is known for its ease of use in mathematical computations and its extensive toolbox for AI and machine learning. Python, on the other hand, has a vast ecosystem of libraries like TensorFlow and PyTorch. The choice depends on your preferences and project requirements.

You can find support, discussion forums, and a community of MATLAB users on the MATLAB website, Matlansolutions forums, and other AI-related online communities. Remember that MATLAB's capabilities in AI and machine learning continue to evolve, so staying updated with the latest features and resources is essential for effective AI development using MATLAB.

Without any hesitation the answer to this question is NO. The service we offer is 100% legal, legitimate and won't make you a cheater. Read and discover exactly what an essay writing service is and how when used correctly, is a valuable teaching aid and no more akin to cheating than a tutor's 'model essay' or the many published essay guides available from your local book shop. You should use the work as a reference and should not hand over the exact copy of it.

Matlabsolutions.com provides guaranteed satisfaction with a commitment to complete the work within time. Combined with our meticulous work ethics and extensive domain experience, We are the ideal partner for all your homework/assignment needs. We pledge to provide 24*7 support to dissolve all your academic doubts. We are composed of 300+ esteemed Matlab and other experts who have been empanelled after extensive research and quality check.

Matlabsolutions.com provides undivided attention to each Matlab assignment order with a methodical approach to solution. Our network span is not restricted to US, UK and Australia rather extends to countries like Singapore, Canada and UAE. Our Matlab assignment help services include Image Processing Assignments, Electrical Engineering Assignments, Matlab homework help, Matlab Research Paper help, Matlab Simulink help. Get your work done at the best price in industry.