How to identify variables in workspace from strings in cell arrays and then make changes to the variables themselves?

조회 수: 2 (최근 30일)
Hi all,
I have a cell array containing strings say for eg {'var1','var2','var3',....'varn'} . Now i have values of all these variables in my workspace.So what i want to do is , first to get values of these variables from workspace.Then i want to compare the lengths of these variables.If their lengths are not equal,i want to make them equal using interpolation.I am so very confused that i cant decide how to go ahead on this.Maybe i need to use regexp,whos,..but i dont know how to use them here.
  댓글 수: 1
Stephen23
Stephen23 2017년 2월 24일
편집: Stephen23 2017년 2월 24일
@sangeet pillai: accessing variable names dynamically will make your code slow, buggy, obfuscated, and hard to debug. There are much better alternatives. Read these to know why this is a bad idea, and what the better alternatives are:
or any of the hundreds of other times that dynamically named variables has been discussed on this forum.

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

채택된 답변

Adam
Adam 2017년 2월 24일
편집: Adam 2017년 2월 24일
The reason you are confused is because naming variables in this way is really not the way to solve anything. I don't know how you got the variables in the first place, maybe you individually named them all, but this is the type of problem you come to when you do that. Naming them this way in the first place is usually where people come unstuck, but even if they succeed in doing so accessing them afterwards is a real pain.
Use a struct with dynamic field names if you really want numbered items or even better just use an array - it is what they are for!
  댓글 수: 17
Stephen23
Stephen23 2017년 3월 2일
편집: Stephen23 2017년 3월 2일
@sangeet pillai: we are glad to help you. Please consider accepting Adam's answer, as a small indication of your appreciation.
sangeet pillai
sangeet pillai 2017년 3월 2일
yes.sure.I was thinking the same but didnt know which answer to accept as all your answers helped me in a way

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Workspace Variables and MAT Files에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by