Macarse
Macarse

Reputation: 93163

Save an array to disk

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?

Upvotes: 3

Views: 370

Answers (3)

TZPike05
TZPike05

Reputation: 1188

Also can depend on what type of file you want to save it to. If you want to save it to a .xls (Excel) file you can use the xlswrite() function. The following is pulled from the documentation for the xlswrite() function:

xlswrite(filename,A,sheet,range) writes to the specified sheet and range. Specify range using the syntax 'C1:C2', where C1 and C2 are two opposing corners that define the region.

Hope this helps, but I do also agree with Luca's answer.

Upvotes: 1

Luca Geretti
Luca Geretti

Reputation: 10286

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

Upvotes: 6

High Performance Mark
High Performance Mark

Reputation: 78344

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 ?

Upvotes: 4

Related Questions