Solved

MATLAB C/C++ API - Update big variable

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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 …
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…
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.
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…

740 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