Changing a string in a matrix to a variable with a value given in another matrix.

조회 수: 2 (최근 30일)
Its pretty simple what i want to do but seems complex on matlab. For example we have two difference types of arrays
if true
% code
Names = {'AB','GH','BC';'BG','FG','CF';'DE','CD','EF'}
Values = [1 2 5;1 4 5;1 5 6]
end
all I need to do is take the Value from the String array and make a variable out of it and then give that variable a value from the Values matrix in the corresponding spot.

채택된 답변

James Tursa
James Tursa 2016년 10월 6일
편집: James Tursa 2016년 10월 6일
Well, you can do this using the eval() function, but it is generally not a good idea to do so. You are popping these variables into the workspace dynamically. How do you expect to deal with them downstream in your code? With more eval() statements? Ugly and difficult to maintain. See this link for alternatives:
E.g.,
eval([Names{2,3} '=' num2str(Values(2,3))])
Do you really want to do stuff like this everywhere in your code when you want to use the variable? Probably not.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by