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

6 visualizzazioni (ultimi 30 giorni)
>> 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?

Risposta accettata

Stephen23
Stephen23 il 26 Gen 2018
Modificato: Stephen23 il 26 Gen 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 Commenti
Daniel Bridges
Daniel Bridges il 27 Gen 2018
Modificato: Daniel Bridges il 27 Gen 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 il 27 Gen 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.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by