0

I get the error

 struct cannot be indexed with {' in Octave.

What I'm trying to do is to save an array in a file by doing:

save myfile.mat initialW

The array seems to be saved ok in the file. Now I want to load it, so I wrote:

numberOfHiddenLayers = length(hiddenLayers);
w = cell(numberOfHiddenLayers + 1, 1);
w = load("myfile.mat");

However this doesn't seem to work. Any help?

myFile.mat looks like this:

# Created by Octave 5.1.0, Sat May 11 23:56:06 2019 GMT <unknown@DESKTOP-4LP8KO2>
# name: initialW
# type: global cell
# rows: 3
# columns: 1
# name: <cell-element>
# type: matrix
# rows: 8
# columns: 3
 0.11396577078177994 -0.35348554375077296 -0.075606765835858347
 -0.57613421825993738 -0.54574495840109649 -0.094554469152263232
 -0.31746528997142315 -0.46945201018320487 -0.119175071441675
 -0.23112104844315234 -0.015682264154657233 0.34987783444013509
 -0.087880331273064327 -0.19609313736912465 -0.08929095997252362
 -0.080653681379305231 0.024968754990479569 0.24949668431650684
 -0.57498895224664437 0.29920144551430461 -0.50083852142549268
 -0.3961249607134249 0.25330869897866082 0.25478925191660773


# name: <cell-element>
# type: matrix
# rows: 8
# columns: 9
 0.0037531259407581619 0.43063491636241652 -0.15445269549506291 0.47739161602226637 0.22753868435063779 -0.020492674304004144 -0.057513893868040733 -0.059060562491990765 -0.25706911999311255
 -0.29424894306440108 0.38336286612135578 -0.16776741033203713 0.46943337027631832 0.13408640078121992 -0.31554901391689794 0.35099674273923137 -0.091832499829088587 0.30933982744981386
 0.52316428666987069 0.27341366734288147 0.51082650460372281 0.49809576149309504 0.37444903641719957 0.38334119916001402 0.42842076982657767 -0.24217696807363465 0.28086557806980544
 -0.073322311691991215 0.63483655613700107 0.27533329919913868 -0.048081459516434499 -0.2267065362144246 0.64395424194737783 0.58767652657219105 0.41793542891042484 0.63321069992358114
 0.65763013238306822 0.02860044065789058 -0.011829632755802955 0.22009594392084614 -0.26511909072695583 0.57370613947506088 0.11415461587493758 0.10573792459512454 -0.32427434895492757
 0.0089604833881801182 -0.073811064790883618 -0.20709124889541619 -0.036291825999526517 0.52062296154868948 -0.011081755811460203 -0.31768168116882467 0.60905440138955669 0.17623419475038399
 -0.27776341169684216 0.61262393897374579 0.065512487276322307 -0.047081972124068172 0.55430062047934725 -0.33326753953006594 0.23202447961516875 0.17662506269733375 0.34004162952881817
 -0.046484818259064919 -0.32230299619629554 0.18841716283341831 -0.18689054271483652 0.026911300930141047 0.58017623550299824 0.12685162090341895 0.29067778228781277 0.56684018254895219


# name: <cell-element>
# type: matrix
# rows: 1
# columns: 9
 0.40736020065140749 0.31486116916450352 0.085860186186408449 -0.10247723839970202 -0.14917985837470693 0.65015717261764294 -0.1038654730028068 0.38554195073868397 -0.039542106657368348
1
  • Have you looked at w? It's a struct after the load Commented May 12, 2019 at 4:29

1 Answer 1

2

When you load a mat-file with a single output argument, it loads the data into a struct with fields that are the names of your variables. w is a struct with field initialW after you call load.

When you assign to a variable without any indexing expression on the left hand side, you discard the previous value. So the cell array your started with is simply overwritten by the struct.

You can do either

w = load(...);
w = w.initialW;

Or, if you want to use the cells:

w = cell(...);
iw = load(...);
w{1:length(iw.initialW)} = iw.initialW;
Sign up to request clarification or add additional context in comments.

2 Comments

initialW is the same type as w. w is an array and I want to fill each position with the data in myfile.mat
Just updated my answer. You should add your first comment to the question. It's much more important than the contents of the file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.