How to read a CDF File?

7 views (last 30 days)
Nur Zawani Rosli
Nur Zawani Rosli on 27 Jan 2023
Edited: Tushar Behera on 30 Jan 2023
I am trying to read a CDF file obtained from SWARM Satellite Data Access. I have been using this code:
data1 = cdfread('SW_OPER_MAGA_LR_1B_20140101T000000_20140101T235959_0602_MDR_MAG_LR.cdf', ...
'ConvertEpochToDatenum', true);
type example.txt
I was able to read the data but the date and time does not appear in the array. I need the time to obtain a specific time where the satellite is available for my research. Additionally, is there a way to only read a specific range of time only? Since the file is too long and it takes a long time to run the whole file.
Thank you :)

Answers (1)

Tushar Behera
Tushar Behera on 30 Jan 2023
Edited: Tushar Behera on 30 Jan 2023
Hi Nur,
I believe that you are trying to read from a ".cdf" file using the "cdfread" function. However, You are not able to find the date and time in the array.
The code you are using for reading the ".cdf" file is correct and the date and time is in the first column, However, it is in "datenum" format as you have set the "ConvertEpochToDatenum" option to "true".
Use the code below to convert "datenum" format to "datetime" format which will give you an array of date and time,
t = datetime(cell2mat(data1(:,1)),'ConvertFrom','datenum')
you can concatenate this array of datetime type data with your original cell type data. A better way to concatenate will be to convert this array to table format along with the cell type data you got from "cdfread". A table data type is best to use when working with different data types.
newdata=[datetimedata originaldata];
Also you can specify range of data by indexing in to the table:
This will give all the rows for the first column of the table. you can edit this code as per your requirements.
I hope this helps you in your work.


Find more on Resizing and Reshaping Matrices 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!

Translated by