0

I have a simple Excel table with two columns. After importing the table into MATLAB, I want the names from column 1 to be declared as variables with corresponding values from column 2.

So in MATLAB, it would look like this:

House = "Villa"
Street = "Grand avenue"
Number = 88
Country = "US"

Example below:

Example below

How to do this?

1 Answer 1

1

I'd use assignin for something like this:

function assignin_me(name, value)
assignin('caller', name, value)
end

Then you can loop over your table t like:

for i = 1:size(t,1)
   assignin_me(t{i,1}, t{i,2});
end

But I would also reconsider storing these values directly in variables anyway. You might have better luck just sticking them in fields in a struct:

s = struct;
for i = 1:size(t,1)
   s.(t{i,1}) = t{i,2};
end
Sign up to request clarification or add additional context in comments.

1 Comment

Many thanks Andrew! I used your suggestion of struct. Only thing I needed to add was converting it to a character array. Works as expected. s = struct; for i = 1:size(t,1) s.(char(t{i,1})) = char(t{i,2}); end

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.