2

I have the following code, I'm trying to create an structure with field names from the cellarray of string.

data1 has the following data, it's a 5x4:

1   5    298      53
2   9    284      35
3   0    582      329
4   17   892      67
45  183  45       29

data1 = xlsread('data1.xlsx');
namesoftags = {'timeaxis','cputime','flux','volts'};
for i =1:4
    S = cell2struct(data1(:,i),namesoftags(i));
end

But it's giving this error:

Error using cell2struct
Unknown command option.

Error in structuredemo (line 4)
    S = cell2struct(data1(:,i),namesoftags(i));

Thank you.

1 Answer 1

3

You a providing a matrix data1(:,i), rather than a cell, to the cell2struct function. However, you don't need this function to accomplish your goal here. Use S.(fieldname) to build your structure instead.

data1 = xlsread('data1.xlsx');
namesoftags = {'timeaxis','cputime','flux','volts'};
for i =1:4
    S.(namesoftags{i}) = data1(:,i);
end

S = 

  struct with fields:

    timeaxis: [5×1 double]
     cputime: [5×1 double]
        flux: [5×1 double]
       volts: [5×1 double]
Sign up to request clarification or add additional context in comments.

1 Comment

@LuisGarcia: If this answer helped you, please consider accepting it, so the question is marked as answered and so Matt gets a bit of rep for his efforts. You should also be able to upvote!

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.