필터 지우기
필터 지우기

How to assign a name to a Table given set of names?

조회 수: 2 (최근 30일)
Edson
Edson 2016년 4월 6일
편집: Stephen23 2016년 4월 6일
Hi folks. I have a long code which runs every time with the number of items a set. For instance I have a set: Subjects = {Name1,Name2,Name3}. My FOR produces 3 different tables/matrixes: A, B and C. So each time the FOR statement runs, it overwrites the tables generated for each different subject. Is there a way to insert a code inside my FOR statement to store each table/matrix (A,B,C) and name each after the subject? For instance, something like this:
Subjects = {'Name1','Name2','Name3'}
for i=1:length(Subjects)
A = magic(2)
B = magic(3)
C = magic(4)
strcat('A_',Subjects(1)) = A % These were my best attempts, But It doesn't work.
strcat('B_',Subjects(2)) = B
strcat('C_',Subjects(3)) = C
end
% Any help much appreciated.
  댓글 수: 2
Adam
Adam 2016년 4월 6일
I assume you mean matrices rather than tables. A table is now a specific data type in Matlab so it is useful to be as specific as you can with terminology. Your code suggests these are simply matrices though.
Stephen23
Stephen23 2016년 4월 6일
편집: Stephen23 2016년 4월 6일
Use Adam's answer, it is the best solution to your problem.
Creating variables dynamically is a really bad way to code, which is explained in lots of threads on this forum:

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

채택된 답변

Ralf
Ralf 2016년 4월 6일
편집: Ralf 2016년 4월 6일
Hi Edson,
would that work for you?
assignin('base', ['A_' Subjects{1}], A);
  댓글 수: 3
Guillaume
Guillaume 2016년 4월 6일
편집: Guillaume 2016년 4월 6일
As a rule, do not use assignin, evalin, eval and similar. Do not create variable names dynamically and do not embed metadata in variable names. The metadata belongs in the content of the variable not its name.
Creating variable names dynamically is slow, is a nightmare to debug, makes subsequent code a lot more complicated, and may also not always work.
Adam's answer is a much better way of doing what you want.
Ralf
Ralf 2016년 4월 6일
Guillaume, you are absolutely right. Assignin and evalin are "quick and dirty" solutions. I must admit that Adam's solution is much better.

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

추가 답변 (1개)

Adam
Adam 2016년 4월 6일
편집: Adam 2016년 4월 6일
Taking your current solution and making minimal changes, rather than starting from scratch...
Subjects = {'Name1','Name2','Name3'}
for i=1:numel(Subjects)
A.( Subjects{i} ) = magic(2)
B.( Subjects{i} ) = magic(3)
C.( Subjects{i} ) = magic(4)
end
will give you structs A, B and C with a field for each subject containing the relevant matrix.
You can then get hold of these fields using dynamic strings in the same way I do above.
  댓글 수: 1
Edson
Edson 2016년 4월 6일
Thanks very much Adam for your answer. I think I can work with Ralf's and yours on my code. Have a great day!

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

카테고리

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