MATLAB generated m-code's importdata function
2 views (last 30 days)
Show older comments
I have a .txt file that I am trying to analyze that has just one number that I am interested in obtaining. It is contained in the headers, which looks like this:
Recorded: 22/06/15 10:01:52
Routing Mask A: 1 0 0 0 0
Routing Mask B: 0 1 0 0 0
Start time [s]: 0.000000
Time span [s]: 0.001584
Counts A: 0
Counts B: 37757
All I'm interested in is gleaning the counts from "Counts B: 37757". I used the MATLAB generated code from manually importing the data file, and it gives me this:
function [s] = importfile(fileToRead1)
DELIMITER = ' ';
HEADERLINES = 9;
newData1 = importdata(fileToRead1, DELIMITER, HEADERLINES);
vars = fieldnames(newData1);
for i = 1:length(vars)
assignin('base', vars{i}, newData1.(vars{i}));
end
s=newData1.data(2);
I'm not very experienced in using the importdata function of MATLAB, so I'm a little confused as to what this is actually doing. Naturally, I went in and changed up the HEADERLINES parameters and looked for some pattern for the output, but it seemed random in the selection of data to output. Would anyone be willing to help me understand this code? Or suggest a better way of importing the counts that I am interested in?
A huge thank you in advance :)
1 Comment
Sean de Wolski
on 24 Jun 2015
You must be using a release pre R2012b; the autogenerated import code got a lot better in 12b.
Answers (1)
dpb
on 24 Jun 2015
It's not clear how many of the blank lines are fignewtons of the display/posting formatting and how many are real; the count of 9 in your code snippet doesn't seem to jibe with the file as given. Given that you get the proper count for the file as it actually exists, I'd punt importdata entirely and simply write
cnts=textread(yourFileToRead,'Counts B: %d','headerlines',N);
I know textread has been deprecated to the status of "red-haired stepchild" by TMW but it is still very useful and handy for simple tasks such as this as
- It takes the filename instead of needing an explicit fopen/fclose which is convenient and
- It returns an "ordinary" double instead of forcing a cast from a cell which is another simplification when there's no need at all for the cell array.
It includes many of the other optional parameters that its big brother textscan does as well but handles some simple cases more simply. The big disadvantage is that if there are multiple sections within the file that must be read, it cannot be called repetitively; hence if there are other data further down in the file that are also of interest then textscan is the better choice and live with the extra complexity.
As for what that code snippet does, it's kinda' typical of computer-generated code; the code written isn't particularly efficient and is not at all what any Matlab user would produce but what it does is to create a set of variables from the strings it finds in the input file and then iterates over those creating in the base workspace those variables the values it parsed for them. It's a very convoluted way to the result and is so owing to that it is a generic piece of code that can handle a general text-value line of input rather than a specific input format string that reflects what is actually a fixed piece of data in the file as I used above.
0 Comments
See Also
Categories
Find more on Data Type Conversion 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!