Hello,
I need to save a .mat variable that has 226 rows and 301 columns in a .bin binary file. What is the way to do this?

 Réponse acceptée

Adam Danz
Adam Danz le 10 Déc 2018

0 votes

5 commentaires

Guilherme Preisser
Guilherme Preisser le 10 Déc 2018
Modifié(e) : Guilherme Preisser le 10 Déc 2018
I tried using this command, but it was saved as vector
Adam Danz
Adam Danz le 10 Déc 2018
I don't know what you mean by "I got one". Could you provide the relevant code and a small sample of the data you want to save?
Stephen23
Stephen23 le 10 Déc 2018
"I tried using this command, but it was saved as vector"
All files are sequential bytes of data. What do you expect to get?
Guilherme Preisser
Guilherme Preisser le 10 Déc 2018
The command I used was this:
fileID = fopen( 'binary.bin' , 'w' );
fwrite (fileID,idx)
fclose (fileID)
The variable idx is a logical array with the dimensions mentioned above. What I want is to save this variable in a binary file. But when I used this command, the binary file was saved as vector.
As Stephen mentioned, this is how a binary file works. Here's an example.
Here we create a matrix and save it to a binary file.
fileID = fopen('MyMatrix.bin','w');
myMat = magic(3)
myMat =
myMat =
8 1 6
3 5 7
4 9 2
fwrite(fileID,myMat);
fclose(fileID);
And now we read it back into matlab and see that it's a columnar vector.
fileID = fopen('MyMatrix.bin');
myMat = fread(fileID)
myMat =
8
3
4
1
5
9
6
7
2
If you know what size the matrix should be, use reshape.
myMat = reshape(myMat, 3,3)

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by