0

Hello I would like to get some help with something please:

I'm trying to make readable a int8 vector (which was actually retrieved from an already binary .mat file via a MySQL database).

I'd like to do something like:

fileId = fopen(fileName, 'w');
fwrite(fileId, vector,'uint8');
fclose(fileId);
load(fileName);

But Matlab throws an error "File may be corrupt".

The idea behind this, is to use a central database to stock our .mat files.

vector is a [ 306624x1 int8] and looks like:

vector = [77;65;84 ...

Thanks for any help

Edit

I'm continuing with this task, the solution that @Rotem gave me works perfectly. I have millions of .mat files, which are really slow to read from a hard drive.

So, I have changed to a database and in order to retrieve these files I do mysql queries (this is much faster).

I recover each file as a vector of bytes, then I write in a local hard drive and reloaded using load().

@Rotem helped me with that, but now I'm facing the problem of space disc. There is a way to perform this conversion directly without passing through fwrite - load?

Thanks for the lights!

3
  • You don't want fwrite, you want save if your goal is a MAT file. Commented Aug 23, 2016 at 12:18
  • Are you trying to create a database of binary representations of .mat files? You are using fwrite when your problem says you are reading in data? Commented Aug 23, 2016 at 12:31
  • Sorry, I wasn't clear enough. The problem is not the reading. My starting point is an uint8-vector which has the bytes of a binary (.mat) file (each component of the vector is a byte). What I'm trying to do is to rebuild the file from this sequence of bytes. That's why my try was to re-write this vector in a binary .mat file and trying to load it to recover the inicial data on the file. Thanks for your comments. Commented Aug 23, 2016 at 13:01

1 Answer 1

1

I can't say I got the motivation behind it...

Check the following code sample:

%Create sample mat file with saved matrix A.
A = magic(3);
save('A.mat', 'A');
clear A; %Note: A is cleared from Matlab Workspace.

%Read A.mat to uint8 vector:
f = fopen('A.mat', 'r');
vector = fread(f, '*uint8'); %Use '*uint8' to keep vector in class uint8.
fclose(f);

%Write vector to B.mat as binary file:
f = fopen('B.mat', 'w');
fwrite(f, vector);
fclose(f);

%Load B.mat as mat file:
load('B.mat');

%Display A matrix (loaded from B.mat).
A

A =

8 1 6
3 5 7
4 9 2

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your help, thanks to you I realized that a typecast was missing in my code: int8 -> uint8.
Thanks for your help @Rotem. Do you know a method to read the data directly from the vector instead? (I edited my question explaning better my issue). Thanks for you help.
I don't know of such method. Can't you simply delete MAT file after loading?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.