How to repeat rows in fields of structure

The existing strucure has to be expanded along rows of fields. The actual structure contains many fieldsnames and rows in them.
s.a=[1;2;3];
s.b=[9 1; 5 4; 3 7];
The rows have to be repeated to certain number of rows say 53.
I have used arrayfun and repmat, but error of size mismatch is shown.
Without use of for loop, how is it possible.

 Risposta accettata

Guillaume
Guillaume il 21 Nov 2018
Modificato: Guillaume il 22 Nov 2018
Note that practically, there is no difference between arrayfun, structfun, etc. and a loop. If anything, arrayfun can be slower than a loop.
I suspect that what you want is structfun which applies the same function to each field of the structure:
news = structfun(@(fld) repmat(fld, 53, 1), s, 'UniformOutput', false)

4 Commenti

Following error is shown: "Error using struct Field names must be non-empty character vectors"
Guillaume
Guillaume il 21 Nov 2018
Modificato: Guillaume il 21 Nov 2018
Major typo! The struct in my example should have been structfun, as you could have infered from the explanation on the line just above.
Another error: Undefined function or variable 'field'. Error in @(fld)repmat(field,53,1). Correction:
news=structfun(@(field)repmat(field,53,1),s,'UniformOutput',false)
However, rows in field reaches to 159 instead of 53.
Sorry yes, I changed the name of the variable midway through the function. I must have been tired when I wrote that answer.
I'm not actually sure I understood what you actually want to do with each field. I thought you wanted to replicate the rows 53 times. If that's not what you want to do, you need to change the body of the function used in structfun to do what you actually want to do.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Operators and Elementary Operations in Centro assistenza e File Exchange

Prodotti

Release

R2017a

Richiesto:

il 21 Nov 2018

Modificato:

il 22 Nov 2018

Community Treasure Hunt

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

Start Hunting!

Translated by