"patient" is a 1x3 struct with "billing" being a fieldname. When I issue patient.billing, I get this:
>> patient.billing
ans =
127
ans =
28.500000000000000
ans =
23.300000000000001
Why is each value displayed separately? How do I collect the output into one variable?

 Réponse acceptée

Walter Roberson
Walter Roberson le 16 Déc 2015

1 vote

[patient.billing]
You are seeing "structure expansion", which is similar to cell array expansion. The output of
patient.billing
is the same as if you had typed
patient(1).billing, patient(2).billing, patient(3).billing, .... patient(end).billing
as a comma separated list. These will be treated as if you had typed separate arguments, so if for example you had used
sum(patient.billing)
that would be the same as
sum(127, 28.5, 23.3)
and it would then try (and fail) to use the 28.5 as the dimension number that sum expects for its second argument.
[patient.billing]
works because [] is the list building operator, and with comma separated lists is equivalent to horzcat()
horzcat(127, 28.5, 23.3)
being well defined

6 commentaires

David C
David C le 16 Déc 2015
This answers my question. Thanks.
Aside: I just realized, after reading the 3rd code block in your answer, that
>> 12,13,14
is actually valid. So is:
>> 12;13;14
Which I expect. But
>> 12 13 14
is not.
I always knew ';' and line breaks are used to separate statements. But it seems ',' works too. Is this use of the comma, to separate statements, documented somewhere?
Walter Roberson
Walter Roberson le 16 Déc 2015
"You also can enter more than one statement on the same line by separating statements. To distinguish between commands, end each one with a comma or semicolon. Commands that end with a comma display their results, while commands that end with a semicolon do not. "
David C
David C le 16 Déc 2015
Great - thanks!
David C
David C le 16 Déc 2015
I just found that if the "patient" structure is the Property of a class, then this doesn't work:
[myClass.patient.billing]
This seems to be a bug?
Walter Roberson
Walter Roberson le 16 Déc 2015
Which version are you using? The behaviour of expansion changed in R2015b.
David C
David C le 17 Déc 2015
I am using R2014b.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Entering Commands dans Centre d'aide et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by