yzt
yzt

Reputation: 1

Use Excel table row names as variable names in MATLAB

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?

Upvotes: 0

Views: 160

Answers (1)

Andrew Janke
Andrew Janke

Reputation: 23858

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

Upvotes: 1

Related Questions