0

This is a solution from another stackoverflow participant who helped me out. Data is coming from a csv file:

    States Damage Blizzards

    Indiana 1      3
    Alabama 2      3
    Ohio    3      2
    Alabama 4      2


%// Parse CSV file
[States, Damage, Blizzards] = textread(csvfilename, '%s %d %d', ...
    'delimiter', ',', 'headerlines', 1);

%// Parse data and store in an array of structs
[U, ix, iu] = unique(States);        %// Find unique state names
S = struct('state', U);              %// Create a struct for each state
for k = 1:numel(U)
    idx = (iu == k);                 %// Indices of rows matching current state
    S(k).damage = Damage(idx);       %// Add damage information
    S(k).blizzards = Blizzards(idx); %// Add blizards information
end

In MATLAB, I need to create a series of assigned variables (A1,A2,A3) in a loop. So I have structure S with 3 fields: state, tornado, hurricane.

Now I have attempted this method to assign A1 =, A2 =, which I got an error because it will not work for structures:

   for n = 1:numel(S)
   eval(sprintf('A%d = [1:n]',S(n).states));
   end

Output goal is a series of assigned variables in the loop to the fields of the structure:

  A1 = 2 3
  A2 = 2 3
  A3 = 4 5
7
  • I'm not sure that this is the 'Matlab-way' of doing things, as explained here Commented May 9, 2013 at 19:53
  • 1
    @user1608954 Why do you need a bunch of variables with different names? Why not create another field in the structure for that, or at least use cell arrays? Commented May 9, 2013 at 20:06
  • Well, I'm generating a different variable name so I can plot the different variables. Schorsch, here is a structure: S = struct('Texas', 0, 'Kansas', 1, 'Maryland', 2) Commented May 9, 2013 at 20:16
  • I will. Give me a minute. Commented May 9, 2013 at 20:35
  • I have posted the entire code. Commented May 9, 2013 at 20:45

1 Answer 1

1

I'm not 100% sure I understand your question.
But maybe you are looking for something like this:

for n = 1:numel(S)
   eval(sprintf('A%d = [S(n).damage S(n).blizzards]',n));
end

BTW using evalc instead of eval will suppress the command line output.

A little explanation, why

eval(sprintf('A%d = [1:n]',S(n).state));  

does not work:

S(1).state  

returns

ans = 
   Alabama

which is a string. However,

A%d

expects a number (see this for number formatting).
Additionally,

numel(S) 

yields

ans = 
   3

Therefore,

eval(sprintf('A%d = [1:n]',n));

will simply return the following output:

A1 =

 1

A2 =

 1     2

A3 =

 1     2     3  

Hence, you want n as a counter for the variable name, but compose the vector of the entries in the other struct-fields (damage and blizzards), again, using n as a counter.

Sign up to request clarification or add additional context in comments.

1 Comment

THAT'S IT. Thanks, Schorsch. I had been working with something similar, but just couldn't complete it. Thanks a lot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.