Solved

MATLAB C/C++ API - Update big variable

Posted on 2013-06-12
5
501 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 33

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
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 …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

786 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