필터 지우기
필터 지우기

Assign rename of structure with input command

조회 수: 2 (최근 30일)
George
George 2014년 8월 6일
댓글: George 2014년 8월 7일
Hello
I am trying to make some of my scripts a bit more easy to use.I am stuck on the rename option of a structure combined with the input command.
What i would like to have is
file=input('Enter the name .dat e.g. "fname.dat" = ')
S = input('Enter a name for the structure = ...')
A=load(file);
S.H1 = A(:,2);
S.H2 = A(:,3);
S.H3 =A(:,4);
Something like that i have but it does not work
how can i finally use the input command in a way that will prompt me and alter the name of the structure?
thank you
  댓글 수: 1
Sara
Sara 2014년 8월 6일
Why do you need the user to input the structure name?

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

채택된 답변

Robert Cumming
Robert Cumming 2014년 8월 6일
I also do not advocate the use of eval.
You could use "assignin", e.g.
S.H1 = A(:,2);
S.H2 = A(:,3);
S.H3 = A(:,4);
userName = 'ABC';
assignin ( 'base', userName, S ); this assuigns S to the variable name ABC
or put it in a sub function:
function AssignmentFunction ( S, userName )
assignin ( 'caller', userName, S )
end
However I cant really think why you want to do this - if you explain in a bit more detail what your end goal is (how are you using these renamed variables) you will get better help...

추가 답변 (2개)

Evan
Evan 2014년 8월 6일
You could use the eval command to do this, but eval can be problematic and usually should be avoided. If you were fine with using nested structures but keep a constant name for the outer structure, you could do this:
S = input('Enter a name for the structure = ...','s')
outerStruct.(S).H1 = rand
This way, you could avoid creating a variable from the string name but still allow some flexibility in structure naming. It would just be a little less neat looking.
  댓글 수: 3
Evan
Evan 2014년 8월 6일
편집: Evan 2014년 8월 6일
This is because you cannot create a variable (in this case a structure) name from a string in that way. You have to use the eval command, which is not recommended. In my example code, I make S the fieldname by using a dynamic field reference, which is why I place the S inside parenthesis.
Another user has submitted an example of how to do this with eval, but I have to agree with him that using it in conjunction with user input is quite risky.
George
George 2014년 8월 7일
Thank you for the answer that really helped, i am using the structures to store simulation data, for variables locations so the structure rename (a bit automated) will allow me to post process the large amount of data i have and create multiple main structures (with sub-structures inside) for every location and season, with out having to open the scipt and rename them

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


Nir Rattner
Nir Rattner 2014년 8월 6일
You can use the “eval” function to effectively convert a string to a variable name:
S=input('Enter a name for the structure = ','s');
eval([S '.H1=A(:,2)']);
That being said, “eval” is almost rarely the best choice because it is inefficient and risky (especially when combined with user input). Without knowing exactly what you are trying to do with the renamed struct, another possibility could be to use a wrapper struct and assign the renamed data as one of its fields:
S=input('Enter a name for the structure = ','s');
wrapper.(S).H1=A(:,2);
  댓글 수: 1
George
George 2014년 8월 6일
I am trying to store seasonal results from some simulations i have done, each sim has 8 outputs and those 8 are divided in months (each month has 8 variable) and there is an corresponding annual one

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

카테고리

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