Call fields from a struct

1 view (last 30 days)
James Alix
James Alix on 14 Oct 2021
Commented: James Alix on 15 Oct 2021
I'm using the tensor toolbox and within that the NMF section. I have everything working perfectly except one thing, the initialisation. It uses a struct:
% INIT : A struct containing initial values. INIT.W and INIT.H should contain
% initial values of W and H of size (m x k) and (k x n), respectively.
% When INIT is not given, W and H are randomly initialized.
I can create the 1x1 struct containing INIT.W and INIT.H of the appropriate sizes with no problem. But when I try and call it, I don't seem to be able to do.
The function looks like this within the toolbox looks like this:
params.addParamValue('init',struct([]),@(x) isstruct(x));
So, I thought it should look like this within the line undertaking the NMF:
[W,H,iter,REC]=nmf(data_nmf,k, 'MIN_ITER',500,'tol',1e-7,'method','anls_bpp','init',struct([init.W, init.H]));
I've been reading all the previous questions on structures, read the matlab pages on structures and nmf and tried lots of variations on the above....but I still don't seem to be able to run it.
I'm clearly making a really daft mistake but I just can't see it! If anyone has used this toolbox and this part of it successfully I'd be most grateful to know how you ran the initialisation.

Answers (1)

Dave B
Dave B on 14 Oct 2021
Edited: Dave B on 14 Oct 2021
I'm a little confused, but I'll venture some guesses....
Do you have a struct init in your workspace (where you're calling the function from) with W and H fields? if so, I think you want:
[W,H,iter,REC]=nmf(data_nmf,k, 'MIN_ITER',500,'tol',1e-7,'method','anls_bpp','init',init);
Or do you have some other variable init in your workspace? If so, what is it?
If you don't have some init in your workspace, and your initial w and h were say 100 and 200:
[W,H,iter,REC]=nmf(data_nmf,k, 'MIN_ITER',500,'tol',1e-7,'method','anls_bpp','init',struct('W',100,'H',200));
that's equivalent to:
[W,H,iter,REC]=nmf(data_nmf,k, 'MIN_ITER',500,'tol',1e-7,'method','anls_bpp','init',s);
  1 Comment
James Alix
James Alix on 15 Oct 2021
Thanks for replying.
I had a struct init in the workspace and so the first answer worked. Can't believe I didn't see that!

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!