Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

MATLAB C/C++ API - Update big variable

Posted on 2013-06-12
5
Medium Priority
?
527 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 35

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
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…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
Suggested Courses

885 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