How do I open .bin file and transform data to .txt file

I would like to open bin file with these specification:
Sampling frequency [Hz]:5000
Datatype: 4-byte float
Channels:57
Samples:3000000
What do I need is to have every channel in one column. I was trying to use fread and fopen but I miss something.
Thanks!

1 Kommentar

Can youi possibly get a file with far fewer samples so it can be uploaded? Needs to be less than 5 MB. What application created the file? Can you just tell it to collect less data?

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Star Strider
Star Strider am 2 Dez. 2023

0 Stimmen

A ‘4-byte float’ is probably 'float32' (possibly 'real*4'), so one of those should work for the ‘precision’ argument in fread.
Use the zip function to enclose it in a .zip file, then use the ‘paperclip’ icon (to the right of the Σ icon) in the top toolstrip to upload it here. Then it be available to work with and help you read it.

10 Kommentare

Would also be interesting, how and/or from whom this bin-file was created.
Thanks for quick reply but I am not able to upload the file, it's over 400 MB. Now I am stuck at this:
That was not what @Star Strider or me requested. It seems you have data in Matlab (85e6 instead of 3e6 numbers). Where does these come from? Can't upload a zip as suggested?
@Lukas — I am not certain what you did to create ‘D’ (what you used to open the .bin file) however it appears to have been imported correctly. (A 400+ MB file is too large to upload here, unfortunately.)
If so, just write ‘D’ to a file using writematrix, giving it the appropriate extension (most likely .txt since you want to save it as a text file). See how that works by then reading it using the readmatrix function to be certain that it imported and saved correctly. It may be necessary to specifically cast it as single since that is what a 4-byte float is.
The file is from very powerful electrophysiology system and every channel should be one ECG channel. I would like to study this signal, but unfortunately the system is not able to export it in different format or shorter record.
f = fopen('FILE_1.bin');
D = fread(f,'double');
fclose(f);
writematrix(D);
B = readmatrix('D.txt');
With this code I managed to write to txt file of just one column but it's not what I am looking for. The variable D should be in 57 columns and and different values.
Instead of using fread(f,'double') use fread(f,'float32') as @Star Strider suggested. Currently you have only 85.5M number instead of 171M numbers you are expecting. Hopefully your matrix D has all of your data you want to examin. Of couse you have to reshape the D-vector according your needs afterwards.
One option could be to use the reshape function before writematrix. If it is supposed to have 57 columns (channels), the reshape call might go something like this —
D = (1:57*5).' % Create A Short Version Of 'D' For Demonstration Purposes
D = 285×1
1 2 3 4 5 6 7 8 9 10
Dr = reshape(D, [], 57)
Dr = 5×57
1 6 11 16 21 26 31 36 41 46 51 56 61 66 71 76 81 86 91 96 101 106 111 116 121 126 131 136 141 146 2 7 12 17 22 27 32 37 42 47 52 57 62 67 72 77 82 87 92 97 102 107 112 117 122 127 132 137 142 147 3 8 13 18 23 28 33 38 43 48 53 58 63 68 73 78 83 88 93 98 103 108 113 118 123 128 133 138 143 148 4 9 14 19 24 29 34 39 44 49 54 59 64 69 74 79 84 89 94 99 104 109 114 119 124 129 134 139 144 149 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 110 115 120 125 130 135 140 145 150
Then, write ‘Dr’ to the file. This assumes that this is the correct way to reshape it and write it, and its total length is an integer multiple of 57 (otherwise reshape will fail, although buffer could still be an option in that event). I have no idea what other necessary metadata might be in the file that would be required to reshape it in some other way, so this is the best I can do.
.
Thank you both! Now it is what I was looking for!
Cheers
As always, my pleasure!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Kategorien

Produkte

Version

R2023b

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by