Why does dir .name output have two answers but numel reports only one element?

3 vues (au cours des 30 derniers jours)
>> NoPtfolders.name
ans =
'1'
ans =
'2'
>> numel(NoPtfolders.name)
ans =
1
NoPtFolder is a 2x1 structure. What is NoPtFolders.name if not a 2x1 cell array? Why does numel report only one element?
  1 commentaire
Stephen23
Stephen23 le 30 Jan 2018
Modifié(e) : Stephen23 le 30 Jan 2018
"What is NoPtFolders.name if not a 2x1 cell array?"
It is a comma-separated list. It is not one variable, and certainly not a cell array. Read more:

Connectez-vous pour commenter.

Réponse acceptée

Stephen23
Stephen23 le 26 Jan 2018
Modifié(e) : Stephen23 le 26 Jan 2018
Here is an in-depth discussion of exactly this behavior:
In summary, it is because the number of outputs is demand-driven, and the outer function numel demands exactly one output from the inner function subsref. The inner function subsref (called by a convenience operator) converted your non-scalar structure NoPtfolders into a comma-separated list of scalar structures, but numel only demands one argument of that list... which is hence one single scalar structure.
Here is a visual interpretation: the convenience operator
S.field
actually calls the function subsref, which returns
S(1).field, S(2).field, s(3).field, ...
and because the inner function always supplies exactly one output argument as an input argument to the outer function then your call is simply equivalent to
numel(NoPtfolders(1).name)
which will be scalar... unless NoPtfolders is empty in which case there are no outputs from subsref and so numel gets no inputs and MATLAB throws an error.
It is not clear what you want to test for, so I cannot suggest what you should be doing. Perhaps
numel(NoPtfolders)
is what you want?
Note that MATLAB always has a demand-driven number of outputs: you can call any function with from 0 to the maximum number of output arguments, and MATLAB will supply as many as you demand from the function. An outer function always demands one argument from an inner function (for reasons discussed in the link at the top of this answer).
  6 commentaires
Daniel Bridges
Daniel Bridges le 27 Jan 2018
Modifié(e) : Daniel Bridges le 27 Jan 2018
When I switched from Windows 10 to Ubuntu 16.04 I had to rewrite code replacing / with \. I see fullfile takes care of that so my code would run on either system, so thank you for this instruction. Why should I use sprintf rather than num2str? The documentation suggests sprintf has more functionality, but if all I need to do is change a counting number to a string, then why not use num2str?
Stephen23
Stephen23 le 27 Jan 2018
@Daniel Bridges: high-level functions like num2str or str2double are very convenient, but inside they are just wrappers around calls to low-level operators like sprintf and sscanf, with some fancy input checking and lots of processing to automatically handle different input sizes/classes/...
Using low-level operators gives you direct control over the conversion (which may be an advantage or a disadvantage, depending on the situation), and by removing the wrapper are also significantly faster.
Use whichever one makes your code clearest for you.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Structures dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by