Final struct result not showing

Hi,
I have the following code in `matlab`:
function test
for i=1:5
struct_C.(sprintf('C%d',i)) = 0
end
Now, when I run the function, I get the following:
>> test
struct_C =
C1: 0
struct_C =
C1: 0
C2: 0
struct_C =
C1: 0
C2: 0
C3: 0
struct_C =
C1: 0
C2: 0
C3: 0
C4: 0
struct_C =
C1: 0
C2: 0
C3: 0
C4: 0
C5: 0
But, I only want the following result:
struct_C =
C1: 0
C2: 0
C3: 0
C4: 0
C5: 0
So, when I type the following I get an error as shown (when I expected that I will get the final result of struct_C:
>> struct_C
??? Undefined function or variable 'struct_C'.
How can I solve this issue?
Thanks.

Respuestas (1)

Matt J
Matt J el 12 de Oct. de 2017

0 votos

You need to return struct_C from the function.

Categorías

Etiquetas

Preguntada:

el 12 de Feb. de 2013

Respondida:

el 12 de Oct. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by