how do i have a structure into a variable when the structure was created in a for loop?

I created function containing a for loop to define the values and names of cards in a deck. This is how i defined everything so far:
if(i>=1 && i<=13)
d(i).val=i;
d(i).suit='spade';
elseif(i>=14 && i<=26)
d(i).val=i-13;
d(i).suit='club';
When I call the function it saves it in ans but when i try to call a specific value (ex: d(2).val) it gives me the an error saying that d is undefined. It does work however if i call it like so: ans(2).val .

Answers (1)

You need to allocate the function output to a variable. It does not have to have the same name as the variable inside the function (in fact this is one of the main advantages of functions compared to scripts):
out = yourfunction(...);
out.val
How to call functions and get output arguments are basic MATLAB concepts that are explained in the introductory tutorials, which are highly recommended for all beginners:

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Tags

Asked:

on 21 Mar 2018

Edited:

on 21 Mar 2018

Community Treasure Hunt

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

Start Hunting!