Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

New to MATLAB?

Convert vector of cells to variables

Asked by Johan

Johan (view profile)

on 17 Jun 2013

Hi! I'm importing data from excel and I'm finding the position of the data of interest using findstr and it works fine. My problem is that I want to get a string from the vector of cells (eg. Temperature) and assigning it a vector of digits. This is how I want it to be:

Text={'Temp', 'Humidity', 'Age'}

num=[20 0.8 2; 22 0.85 2.1; 23 0.9 2.2]

Temp=[20 22 23]

Thankful for help Regards Johan

0 Comments

Johan

Johan (view profile)

Products

No products are associated with this question.

3 Answers

Answer by Azzi Abdelmalek

Azzi Abdelmalek (view profile)

on 17 Jun 2013
Accepted answer
For k=1:numel(Text)
   assignin('base',Text{k},num(:,k))
end

2 Comments

Johan

Johan (view profile)

on 17 Jun 2013

Cheers!

Jan Simon

Jan Simon (view profile)

on 17 Jun 2013

Such remote controlled assigments have severe disadvatnages: They decrease the processing speed massively because the JIT cannot understand the program at compile time anymore. And the debugging suffers even more. Therefore I strongly recommend to follow Walter's answer.

Azzi Abdelmalek

Azzi Abdelmalek (view profile)

Answer by Andrei Bobrov

Andrei Bobrov (view profile)

on 17 Jun 2013
Edited by Andrei Bobrov

Andrei Bobrov (view profile)

on 17 Jun 2013

use dataset array

Text={'Temp', 'Humidity', 'Age'}
num=[20 0.8 2; 22 0.85 2.1; 23 0.9 2.2]
d = mat2dataset(num,'VarNames',Text);
>> d.Temp
ans = 
         20
         22
         23

or

d2 = cell2struct(num2cell(num,1),Text,2);
d2.Temp
ans = 
         20
         22
         23

0 Comments

Andrei Bobrov

Andrei Bobrov (view profile)

Contact us