Solved

MATLAB C/C++ API - Update big variable

Posted on 2013-06-12
5
490 Views
Last Modified: 2016-03-02
Hi there,

I have just realized that if I have a variable in a MAT-file (big array) and I want to update this variable by appending a new row (suppose a 2D array) using the MATLAB C/C++ API then it is necessary to load the variable into memory, create a copy with the new row and finally overwrite the existing array with the new one.

Because I am working with big/huge arrays I would like to know how to update a variable without loading the whole thing into memory.

Thank you.
0
Comment
Question by:ScreenFox
  • 2
  • 2
5 Comments
 
LVL 32

Expert Comment

by:sarabande
ID: 39241292
you probably have to use the cat function. see

http://radio.feld.cvut.cz/matlab/toolbox/mathlib/cppmathug/workin15.html

Sara
0
 
LVL 15

Expert Comment

by:yuk99
ID: 39267174
Not sure if you can use it with C/C++ API, but MATLAB has the MATFILE object that allows you to associate at with variables in mat-file, so you can read and update them without loading the whole thing into memory.

Here is an example:

>> x = rand(1000);
>> save x.mat x
>> matObj = matfile('x.mat','Writable',true)
matObj =
  matlab.io.MatFile

  Properties:
      Properties.Source: 'x.mat'
    Properties.Writable: true                                          
                      x: [1000x1000 double]                            

  Methods

>> whos x matObj %compare the Bytes
  Name           Size                Bytes  Class                Attributes

  matObj         1x1                   112  matlab.io.MatFile              
  x           1000x1000            8000000  double                        

>> matObj.x = [matObj.x; 1:1000]; %adding a row
>> matObj
matObj =
  matlab.io.MatFile

  Properties:
      Properties.Source: 'x.mat'
    Properties.Writable: true                                          
                      x: [1001x1000 double]                            

  Methods

>> clear matObj %the mat file will still exist
0
 

Accepted Solution

by:
ScreenFox earned 0 total points
ID: 39280712
Thanks yuk99, but I don't know either.

Anyway, not sure if Matlab actually loads the x matrix when used on a right hand side expression like matObj.x = [matObj.x; 1:1000] or it tries to map to memory the relevant part of the file.
0
 
LVL 15

Expert Comment

by:yuk99
ID: 39282140
Good question and I don't know the exact answer. It requires some tests.
You can try profiling memory usage as described here:
http://undocumentedmatlab.com/blog/profiling-matlab-memory-usage/
I'll try it later when I'll have more time.
0
 

Author Closing Comment

by:ScreenFox
ID: 39329156
Could not find a proper solution.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to implement Singleton Design Pattern in Java.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now