Solved

MATLAB C/C++ API - Update big variable

Posted on 2013-06-12
5
510 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 34

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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
Part One of the two-part Q&A series with MalwareTech.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

623 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