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

>> 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 Comment

"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:

Sign in to comment.

 Accepted Answer

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 Comments

Indeed, I had chosen to use numel(NoPtfolders), because I am trying to obtain a count of the number of folders that contain data to be accessed to be used in a for loop that will process the data in each folder.
I actually wonder if there is a better approach -- I still don't know how to process all data except by counting the number of files and using a for loop. I suspect there is some way to index all without having to count them ...
"I actually wonder if there is a better approach "
Indexing in a loop is very efficient (with preallocated arrays), easy to write and debug, and is standard practice in MATLAB. In fact indexing and arrays is what MATLAB does best. You will waste your time trying to reinvent the wheel:
Thank you! Affirmation is welcome -- though it looks like I need to learn more about sprintf. I have been using string concatenation,
pathtofiledata = [mainfolder,'/',num2str(PtLoop),'/',num2str(PortNo),'.dcm'];
Use fullfile to create filepaths from foldernames (it handles the file separator character for you):
fullfile(mainfolder, num2str(PtLoop), [num2str(PortNo),'.dcm']);
You could use sprint for the filename:
fullfile(mainfolder, num2str(PtLoop), sprint('%d.dcm',PortNo));
or the folder name as well:
fullfile(mainfolder, sprintf('%d',PtLoop), sprint('%d.dcm',PortNo));
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?
@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.

Sign in to comment.

More Answers (0)

Categories

Asked:

on 26 Jan 2018

Edited:

on 30 Jan 2018

Community Treasure Hunt

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

Start Hunting!