using fopen/fread/fclose, how can I do this? please help me to figure it out.
Show older comments
Reading binary files. The file “EMGData.bin” is a binary file that contains EMG responses to microstimulation of primary motor cortex in a cat. This file also contains data that indicates when the stimulation pulses were applied. The file is a total of 44028 elements in length. The first half of the file is the EMG data (22014 elements) stored as 16-bit signed integers. The second half of the file is the stimulation data (22014 elements) stored as 8-bit signed integers. The data was written using little-endian machine format. The sampling rate is 2000 samples/second and the amplitude represents voltage in units of uV. Write an m-file script that uses fopen/fread/fclose to read in the data from “EMGData.bin”. Plot both the EMG and stimulation data from this file and overlay each curve in the same figure using the hold function. Make sure your x-axis is in units of seconds. Label the axes and title your plot appropriately.
Answers (1)
Walter Roberson
on 21 Feb 2011
fid = fopen(Filename,'r');
EMGresponses = fread(fid, 22014, 'int16');
EMGtimes = fread(fid, 22014, 'int8');
fclose(fid);
However, the description of what the two halves mean is broken. In the third sentence it indicates that the second half of the file is the times, but 8 unsigned bits of time can only accomodate 128 different times, not the 22014 that are said to be present. Furthermore, if the second half of the file is the times, then it would not make any sense to plot two curves in the same figure.
2 Comments
Jan
on 21 Feb 2011
@Walter: Doesn't the question sound like a homework? Should we solve this for Justin? What does his teacher think about this?
Walter Roberson
on 21 Feb 2011
Reading the data is the trivial part, described in several of the Answers.
Dealing with the fact that the description of what is in the file is nonsensical is well worthy of homework.
Categories
Find more on Get Started with MATLAB 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!