I want to read the header data. The data file is attached. In the fffffff row there are two data values that I need to read and also in the qqqqqq column there is only one data. How can i read this?

1 visualizzazione (ultimi 30 giorni)
The data file is attached. In the fffffff row there are two data values that I need to read and also in the qqqqqq column there is only one data. How can i read this? Please see the attached file.
  4 Commenti
Image Analyst
Image Analyst il 5 Giu 2014
You still didn't attach it. You attached a screenshot of it displayed in your screen. We need to see something with a .nc extension, not .jpg or other image format.

Accedi per commentare.

Risposta accettata

dpb
dpb il 5 Giu 2014
... qqqqqqqq which has a numeric data (83.58) after |. I have almost 10000 files ...
Presuming there's a consistent naming convention or they're all in a single subdirectory, the easy way to do that is...
d=dir('filnameroot*.dat'); % fix wildcard name to match to get the desired files
n=length(d); % how many we found
dates=zeros(n,7); % preallocate for that many values, one from each file
qvals=zeros(n,1);
for i=1:n
[dates(i,:) qvals(i)]=readqqq(d(i).name);
end
where readqqq is the stuff from above turned into an m-file--
[datedata,qdata] = function readqqq(fn)
fid=fopen(fn,'r');
fgetl(fid)
l=fgetl(fid);
datedata=sscanf(l(strfind(l,'|')+1:end),'%4d-2d-2d %2d:%2d:%2d+%2d');
fgetl(fid)
l=fgetl(fid);
qdata=sscanf(l(strfind(l,'|')+1:end),'%f');
fclose(fid);
Here I've made two return variables; the date data as datedata and from the 'q' line as qdata. So the first is a 7-element vector and the other a scalar. You can rearrange that storage to suit.
As for how to become adept w/ Matlab, a lot depends on whether you've had any programming experience before or not. If you have some, simply reading the syntax and skimming thru the documentation is probably the best bet. If not, you probably could do w/ a text but I've no direct recommendations to give, unfortunately.
There's a series of tutorials and other stuff at
but I've never looked at any so can't tell you what they're like. There's a whole section on Matlab-related books as well. The basic starting point for the documentation is on your system by typing
doc
and then the "Getting Started" link
  4 Commenti
dpb
dpb il 5 Giu 2014
a) A text file is an ASCII file...
b) work thur from inside out --
strfind(l,'|') locates position of the '|' character;
adding one is the next location past it,
and l(fromthere:end) is the trailing portion of the string
Again, can't emphasize enough--just "try it" at the command line and see what something does if it isn't clear from the doc's...

Accedi per commentare.

Più risposte (2)

dpb
dpb il 4 Giu 2014
fid=fopen('yourfile','r');
fgetl(fid)
l=fgetl(fid);
datedata=sscanf(l(strfind(l,'|')+1:end),'%4d-2d-2d %2d:%2d:%2d+%2d');
fgetl(fid)
l=fgetl(fid);
data=sscanf(l(strfind(l,'|')+1:end),'%f');
Will have y,m,d,h,mn,s,tzoffset in datedata array and the floating pt value in data
Can do w/ textscan and 'headerline' but the fgetl call serves the purpose here.
  3 Commenti
anton fernando
anton fernando il 4 Giu 2014
You code works well. The problem is I want to get the data in qqqq column as a numerical value as i am expecting to use it to plot a graph. Thank you.
dpb
dpb il 4 Giu 2014
There is no column qqqqq?
The returned values for the header row are numeric; datedata are integer-valued while data isn't but they're all default doubles in Matlab.
If the question is to then read the tabular data, just skip a couple more lines and then since there's no formatting or skiping any text or the like, just a regular array, fscanf can return the full array in one swell foop.
As for what happens, read it from inside out after executing the first few lines at the command window to get a line into variable l. Look up strfind if you can't decipher what it's doing just from the name and arguments and
doc colon
doc end
for the addressing. The format strings are all described in the doc for fscanf and friends...there's no replacement for simply learning syntax and functionality in Matlab by reading the documentation and experimenting on the command line to see how something works.

Accedi per commentare.


anton fernando
anton fernando il 4 Giu 2014
Modificato: anton fernando il 4 Giu 2014
Thank you very much the long explanation. Actually my question is in the data set that I uploaded has a row with starting qqqqqqqq which has a numeric data (83.58) after |. I have almost 10000 files like that. I want to get that data from each file and use to plot a graph. I know how to get that data from each file after I could read this data from this file. Also if you could please tell me how to start learning Matlab from the beginning and learn all these hardcore coding. Is there any website or whatever to use? Thank you for your time.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by