Convert string to variable.

조회 수: 33 (최근 30일)
DKonk
DKonk 2022년 7월 14일
편집: Stephen23 2022년 7월 14일
I'm pulling a string from a table 'Matrix1'. Before that, I have defined Matrix1 as an actual matrix. What I want to do is pull the string from the table and have matlab recognize the name and use the matrix. I have to pull it from the table, because the table is changing and may sometimes say 'Matrix2' or 'Matrix3'.
So what I have now is a table (named 'Table') and a column (named 'Territory'). I'm using the command Table.Territory(3) to access the third territory in the list. But it comes through as a string and I need it to come through as a matrix. I have already saved that territory name as a matrix.
  댓글 수: 3
DKonk
DKonk 2022년 7월 14일
I'm not entirely sure what a dynamic variable is, but I'm fairly sure I dont want to do this. So I have an excel doc that will be changing frequently (maybe this makes it dynamic?). In my code I will have:
US_matrix = [1 0 0; 0 1 0]
UK_matrix = [0 0 1; 1 0 0] % Just example matrices
Now I need to pull a string from the table, either 'US_matrix' or 'UK_matrix' and access the respective matrix.
Stephen23
Stephen23 2022년 7월 14일
편집: Stephen23 2022년 7월 14일
"I'm not entirely sure what a dynamic variable is, but I'm fairly sure I dont want to do this."
Yet it seems to be exactly what you are attempting. Which would force you into writing slow, complex, inefficient, obfuscated, insecure, buggy code which is hard to debug... just to access your data:
Best avoided.
"So I have an excel doc that will be changing frequently (maybe this makes it dynamic?)"
No, the fact your file can change is unrelated.

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2022년 7월 14일
편집: Stephen23 2022년 7월 14일
This is the cause of your problems right here:
US_matrix = [1 0 0; 0 1 0]
UK_matrix = [0 0 1; 1 0 0]
Having meta-data in variable names is a big sign that you are doing something wrong. Remember, meta-data is data, and data belongs in variables (i.e. arrays) and not in variable names.
As soon as you define lots of separate variables like that, each with their own name, then you have really painted yourself into a dark corner: then you must write slow, complex, inefficient code just to access your variable names dynamically. Ouch. The simple, very efficient, and recommended solution is to use arrays and indexing to access your data (because that is how MATLAB works), for example here are some much better approaches:
Method one: cell array and indexing (recommended):
want = 'UK'; % you get this from the Excel file, or wherever.
data = {[1,0,0;0,1,1],[0,0,1;1,0,0]};
name = { 'US', 'UK'};
idx = strcmpi(name,want);
data{idx} % this is how MATLAB works efficiently, using indexing. Not like your approach.
ans = 2×3
0 0 1 1 0 0
Method two: non-scalar structure (recommended):
A(1).name = 'US';
A(1).data = [1,0,0;0,1,1];
A(2).name = 'UK';
A(2).data = [0,0,1;1,0,0];
idx = strcmpi({A.name},want);
A(idx).data
ans = 2×3
0 0 1 1 0 0
Method three: scalar structure with dynamic fieldnames (still a bit fragile, probably best avoided):
B.US = [1 0 0; 0 1 0];
B.UK = [0 0 1; 1 0 0];
B.(want) % dynamic fieldname
ans = 2×3
0 0 1 1 0 0
Note:
  • Some of these could even be vectorized very easily (e.g. ISMEMBER).
  • You could do something similar using tables: https://www.mathworks.com/help/matlab/tables.html
  • Remember that meta-data is data (and shoud not be part of a variable name).
  댓글 수: 1
DKonk
DKonk 2022년 7월 14일
Method 1 works like a charm. You helped a lot and simplified my code. Much appreciated!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Cell Arrays에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by