필터 지우기
필터 지우기

Naming Variable from input arguments

조회 수: 2 (최근 30일)
xplore29
xplore29 2013년 6월 30일
My function takes two input arguments Dim,Snr I want to name my variable based on these two arguments in the following manner
Matrix_Dim{Snr}
for example if Dim=4, Snr=3 My cell variable should be named Matrix_4
Matrix_4{3}=somexyz where somexyz is the output which i want to store in Martix_4{3}
eval(['matrix_' num2str(DIM) '= somexyz']) will not create the cell array which i actually want to do.
how can i do that.

답변 (2개)

Shashank Prasanna
Shashank Prasanna 2013년 6월 30일
Try using {somexyz} instead of somexyz so that it is a cell.

Walter Roberson
Walter Roberson 2013년 6월 30일
  댓글 수: 2
xplore29
xplore29 2013년 6월 30일
Thank you for the reference. I saw this post before posting the question. This post addresses the issue of assigning result to appropriate cell locations and does not talk about naming the variable dynamically. In my case, I have to first name the variable dynamically and then assign the result in the appropriate cell. I know how to do these two things separately. The problem comes when i try to do these two together.
Walter Roberson
Walter Roberson 2013년 6월 30일
편집: Image Analyst 2013년 6월 30일
Don't do it.
Matrix{Dim}{Snr} = somexyz; %cell array
or
Matrix(Dim).Snr(Snr) = somexyz; %structure
Building variable names on the fly will always get you in trouble.
If you need the variable names to be separate for the purposes of save() then still do not construct the variable names on the fly. Instead,
matrices.(sprintf('matrix_%d', DIM)){Snr} = somexyz;
and then
save('FileName.mat', '-struct', 'matrices')
The post does address naming variables dynamically: it says DON'T, and shows the alternatives. And if you read it right through, it does show a way it can be done in the quite rare circumstance that it is needed.

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by