필터 지우기
필터 지우기

Name a variable that includes the cell name from a cell vector

조회 수: 2 (최근 30일)
Syeda Amberin
Syeda Amberin 2019년 3월 26일
편집: Stephen23 2019년 7월 2일
Hi,
I am trying to assign names to the tables I am creating. I have a 1 by 20 cell vector called "Events" with each cell containing events like "NATO", "G20" etc. Now I need my tables to have names extracted from the cells with extentions like "NATO_a", "NATO_b" etc. I have tried Events(1)_a=table(); but it's not working. How do I extract the cell names and add extensions? Please advise.
  댓글 수: 11
Syeda Amberin
Syeda Amberin 2019년 7월 2일
Thanks, Walter. That was very helpful.
Stephen23
Stephen23 2019년 7월 2일
편집: Stephen23 2019년 7월 2일
@Syeda Amberin: note that putting meta-data into fieldnames will make your code fragile: consider what your code would do if one of the names was 1ABC, or AB@ (i.e. not a valid fieldname). Meta-data is data, and generally it should be stored in a variable, not in a fieldname or variable name.
Your code would be more robust if you just used indexing.

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

답변 (1개)

Syeda Amberin
Syeda Amberin 2019년 3월 26일
I figured a round about.
s1='_a';
s=strcat(Events(1),s1);
Producing:
'Nato_a'
Thanks,
  댓글 수: 2
Walter Roberson
Walter Roberson 2019년 3월 26일
s = [Events{1} '_a']
but note this does not create a table variable with that name.
Syeda Amberin
Syeda Amberin 2019년 3월 27일
Yes, you're right, it doesn't. Thanks.

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by