Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save an array to disk

Tags:

file-io

matlab

I have a Matlab function that creates a cell array with matrixes inside. It looks like this:

>> ind

ind = 

    [10x3  double]
    [10x11 double]
    [ 1x11 double]

>> ind{1}

ans =

   -0.0407    0.1806    0.3175
   -0.1630   -0.0477    0.3487
   -0.1704   -0.0903    0.2375
    0.4861   -0.0547    0.3547
    0.4440    0.1793    0.3329
   -0.3888   -0.0768   -0.3908
   -0.0429    0.2418    0.6098
    0.0263   -0.3948   -0.2316
    0.3766    0.5255   -0.1580
   -0.4005   -0.2788   -0.4579

I want to be able to save this cell array to a file and afterwards loading it.

Which is the fastest way to do this? Is there a way to avoid doing it by hand?

like image 865
Macarse Avatar asked Apr 13 '26 23:04

Macarse


2 Answers

The general solution is simple:

myvar = ind{1};
save myfilename myvar; 
load myfilename;

If no variables are specified, save/load deal with all the variables in the workspace/file. You can save/load multiple specific variables:

save myfilename;
save myfilename myvar1;
save myfilename myvar2;
save myfilename myvar1 myvar2;

load myfilename;
load myfilename myvar1;
load myfilename myvar2;
load myfilename myvar1 myvar2;

Since variables in files are named so to be retrieved out of order, you cannot save the result of an expression, but must always pass through a variable (as the answer does).

like image 95
Luca Geretti Avatar answered Apr 16 '26 22:04

Luca Geretti


I don't know if the in-built function save is the fastest way, but why don't you use it until you get a better suggestion ?

like image 35
High Performance Mark Avatar answered Apr 16 '26 21:04

High Performance Mark



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!