Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
output to file a large data structure

Subject: output to file a large data structure

From: Dimitar Dimitrov

Date: 24 Sep, 2007 12:35:17

Message: 1 of 5

Hello,

I have a Matlab function that outputs a structure Sm(i) with
i = 1000.
Inside there are 10 fields and each of them represents a
matrix with different dimensions (a typical dimension is
300x150) (hence, quite a lot of data).

I would like to write (in an efficient way) the contents of
this structure to a file(s) and then read this file(s) in a
C program and initialize a structure Sc with this data.
(assuming that the C structure Sc has the same fields as the
Matlab structure Sm).

I am sure that this is a standard problem, and I googled it,
 but I couldn't find a reasonable solution.

Thank you,
Dimitar Dimitrov

Subject: output to file a large data structure

From: Adam

Date: 24 Sep, 2007 14:22:55

Message: 2 of 5

"Dimitar Dimitrov" <mail_mitko@mathworks.com> wrote in
message <fd8au5$t5g$1@fred.mathworks.com>...
> Hello,
>
> I have a Matlab function that outputs a structure Sm(i) with
> i = 1000.
> Inside there are 10 fields and each of them represents a
> matrix with different dimensions (a typical dimension is
> 300x150) (hence, quite a lot of data).
>
> I would like to write (in an efficient way) the contents of
> this structure to a file(s) and then read this file(s) in a
> C program and initialize a structure Sc with this data.
> (assuming that the C structure Sc has the same fields as the
> Matlab structure Sm).
>
> I am sure that this is a standard problem, and I googled it,
> but I couldn't find a reasonable solution.
>
> Thank you,
> Dimitar Dimitrov

One option:
help cdfwrite

~Adam

Subject: output to file a large data structure

From: Dimitar Dimitrov

Date: 24 Sep, 2007 15:46:12

Message: 3 of 5

Adam, thank you for the post,

I did not know about the CDF format. It seems relatively
easy to manipulate in Matlab, however, I couldn't find a
library that will import it in a C class (C++ would be
OK as well) (at least not without having to install
additional packages) or am I wrong?

To be honest, I was hoping for a more "standard" solution :)

Regards,
Dimitar Dimitrov


"Adam " <not.my.email@mathworks.com> wrote in message
<fd8h7v$31q$1@fred.mathworks.com>...
> "Dimitar Dimitrov" <mail_mitko@mathworks.com> wrote in
> message <fd8au5$t5g$1@fred.mathworks.com>...
> > Hello,
> >
> > I have a Matlab function that outputs a structure Sm(i) with
> > i = 1000.
> > Inside there are 10 fields and each of them represents a
> > matrix with different dimensions (a typical dimension is
> > 300x150) (hence, quite a lot of data).
> >
> > I would like to write (in an efficient way) the contents of
> > this structure to a file(s) and then read this file(s) in a
> > C program and initialize a structure Sc with this data.
> > (assuming that the C structure Sc has the same fields as the
> > Matlab structure Sm).
> >
> > I am sure that this is a standard problem, and I googled it,
> > but I couldn't find a reasonable solution.
> >
> > Thank you,
> > Dimitar Dimitrov
>
> One option:
> help cdfwrite
>
> ~Adam

Subject: output to file a large data structure

From: Dimitar Dimitrov

Date: 24 Sep, 2007 22:00:23

Message: 4 of 5

Actually, let me ask the question in a different way
(with a simple example)!

If A = [1 2 3 4; 5 6 7 8],

I can output "A" in a binary file 'matlab.bin' like this:

fwriteid = fopen('matlab.bin','w');
count = fwrite(fwriteid,a,'double');
status = fclose(fwriteid);

Then, I can read this data from a C++ program using:

double A[8];
ifstream fin("matlab.bin", ios::binary);
fin.read((char *)(&A), sizeof(A));
fin.close();

Now if I want to do the same thing for a structure
S.A = [1 2 3 4; 5 6 7 8],
I cannot use fwrite and have to use: save my_file.mat S,
however I couldn't read the structure in "my_file.mat" using
the standard C++ I/O functions.
If I can read S.A from "my_file.mat" this would solve my
problem!

Any suggestions?

Thanks
Dimitar Dimitrov
 




"Dimitar Dimitrov" <mail_mitko@mathworks.com> wrote in
message <fd8m44$dst$1@fred.mathworks.com>...
> Adam, thank you for the post,
>
> I did not know about the CDF format. It seems relatively
> easy to manipulate in Matlab, however, I couldn't find a
> library that will import it in a C class (C++ would be
> OK as well) (at least not without having to install
> additional packages) or am I wrong?
>
> To be honest, I was hoping for a more "standard" solution :)
>
> Regards,
> Dimitar Dimitrov
>
>
> "Adam " <not.my.email@mathworks.com> wrote in message
> <fd8h7v$31q$1@fred.mathworks.com>...
> > "Dimitar Dimitrov" <mail_mitko@mathworks.com> wrote in
> > message <fd8au5$t5g$1@fred.mathworks.com>...
> > > Hello,
> > >
> > > I have a Matlab function that outputs a structure
Sm(i) with
> > > i = 1000.
> > > Inside there are 10 fields and each of them represents a
> > > matrix with different dimensions (a typical dimension is
> > > 300x150) (hence, quite a lot of data).
> > >
> > > I would like to write (in an efficient way) the
contents of
> > > this structure to a file(s) and then read this file(s)
in a
> > > C program and initialize a structure Sc with this data.
> > > (assuming that the C structure Sc has the same fields
as the
> > > Matlab structure Sm).
> > >
> > > I am sure that this is a standard problem, and I
googled it,
> > > but I couldn't find a reasonable solution.
> > >
> > > Thank you,
> > > Dimitar Dimitrov
> >
> > One option:
> > help cdfwrite
> >
> > ~Adam
>

Subject: output to file a large data structure

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 24 Sep, 2007 22:10:29

Message: 5 of 5

In article <fd9c1n$rng$1@fred.mathworks.com>,
Dimitar Dimitrov <mail_mitko@mathworks.com> wrote:
>Actually, let me ask the question in a different way
>(with a simple example)!

>If A = [1 2 3 4; 5 6 7 8],

>I can output "A" in a binary file 'matlab.bin' like this:

>fwriteid = fopen('matlab.bin','w');
>count = fwrite(fwriteid,a,'double');
>status = fclose(fwriteid);

>Then, I can read this data from a C++ program using:

>double A[8];
>ifstream fin("matlab.bin", ios::binary);
>fin.read((char *)(&A), sizeof(A));
>fin.close();

>Now if I want to do the same thing for a structure
>S.A = [1 2 3 4; 5 6 7 8],
>I cannot use fwrite and have to use: save my_file.mat S,
>however I couldn't read the structure in "my_file.mat" using
>the standard C++ I/O functions.

I suggest that you write out field by field, prefixed by some
size (and possibly type) information.

fn = fieldnames(A);
for K=1:length(fn)
  fwrite(fwriteid,size(size(A)),'long'); %number of dimensions
  fwrite(fwriteid,size(A.(fn)),'long'); %dimensions
  fwrite(fwriteid,A.(fn),'double'); %data
end
--
  Is there any thing whereof it may be said, See, this is new? It hath
  been already of old time, which was before us. -- Ecclesiastes

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us