Structure array and file operations
3 views (last 30 days)
Show older comments
Hello,
I am reasonably new to MATLAB and am stuck on a question regarding structure arrays. So basically it wants me to get MATLAB to read data from a .txt file, and return values with a title, these values are number of triangles (1st value), height and width (alternating values, 2nd is height, 3rd width etc), along with this it also needs to calculate the area of the triangle. Now, this data is quite randomized but apparently Matlab will be able to read it. I've really only got the basics but I am guessing I will need to use fscanf and readf? The struct function is simple enough i just dont know how to mix the three. I have uploaded the question along with a photo of some of the data.


Thanks, Regards Steve
0 Comments
Accepted Answer
Stephen23
on 27 Apr 2015
Edited: Stephen23
on 29 Apr 2015
You can find a list of functions that can read textfiles here:
I would recommend that you use textscan, which can also convert the string data to the numeric values. However that sample file is a bit mixed-up, and does not have a simple layout. You might need to read the first part of the file line-by-line using some low level functions, and then the rest of the file using textscan. Here is a list of the low-level file-reading functions:
you might find fgetl very useful for this purpose!
Also note that if you keep a file open then (most of) these functions continue to read the file from the point where the last function finished, so you can do things like this:
fid = fopen(filename,'rt');
A = fgetl(fid);
B = fgetl(fid);
C = textscan(fid,...);
...
fclose(fid);
More Answers (0)
See Also
Categories
Find more on Data Import and Analysis in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!