0

I am trying to decrease some big chucks of matlab code i had from a while ago, and was hoping to get them a bit more "clean".

The VarName2,VarName3,VarName4 ...etc are provide by measured data and i will know what they are always going to be thus i gave me the name A,B ,C , the think i want changed though is the first part of the name, so every time i run the .m file I will use the input('') option

where as fname = 'SWAN' and A, B , C are the second part of the name and they are constant.

fname = input ('enter name')

fname_A = VarName2
fname_B = VarName3
fname_C = VarName4

and want to be getting

SWAN_A = VarName2
SWAN_B = VarName3
SWAN_C = VarName4

thank you

Following your advices I been trying the structure construction

S.name = input ('enter name of the data ".." ==')
S.A = A;
S.A(1,:)=[];
S.B = B;
S.B(1,:)=[];
S.C = C;
S.C(1,:)=[];
S.D = D;
S.D(1,:)=[];
S.E = E;
S.E(1,:)=[];

may i ask if i can also have an input thing command so i can change the name of the structure?

Precede the script with S='west' and then do

'S'.name = input ('enter name of the data ".." ==')
    S.A = A;
10
  • What exactly is the problem? Can't you simply do a replace all for fname_a into SWAN_A? Commented Jul 8, 2014 at 11:59
  • 1
    @George: dont do this, use cell arrays or structs instead: matlab.wikia.com/wiki/…, blogs.mathworks.com/videos/2010/03/08/… (number 1 on the list) Commented Jul 8, 2014 at 12:02
  • @DennisJaheruddin i was doing that bu t i want to automate the process a bit its annoying to have to open the files all the time and change the name Commented Jul 8, 2014 at 12:03
  • 2
    @DennisJaheruddin: no, the OP is trying to assign variables where the name is defined at runtime! I still say don't do that, use a structure instead in this case... Commented Jul 8, 2014 at 12:08
  • 1
    @George Why would you want this? It seems like an extremely bad idea. What are you doing with SWAN_A etc later in your code? If you name them at run time, how do you plan on working with them later? And if you're not working with them later, why would you need them to be named like that? I think you should rethink the structure of your code here. Commented Jul 8, 2014 at 12:12

2 Answers 2

3

Here is how I would probably store the information that you are handling:

S.name = input ('enter name')
S.A = VarName2
S.B = VarName3
S.C = VarName4

And if you want to do it a few times:

for t=3:-1:1
  S(t).name = input ('enter name')
  S(t).A = VarName2
  S(t).B = VarName3
  S(t).C = VarName4
end

In this way you could now find the struct named 'swan':

idx = strcmpi({S.name},'SWAN')
Sign up to request clarification or add additional context in comments.

Comments

1

you can use eval

eval( sprintf('%s_A = VarName2;', fname ) );
eval( sprintf('%s_B = VarName3;', fname ) );
eval( sprintf('%s_C = VarName4;', fname ) );

Note that the use of eval is not recommended.

One alternative option may be to use struct with dynamic field names:

A.( fname ) = VarName2;
B.( fname ) = VarName3;
C.( fname ) = VarName4;

Now you have three structs (A, B and C) with A.SWAN equal to VarName2, B.SWAN equal to VarName3 etc.

7 Comments

thanks it works fine, exactlyy what i wanted, much appreciated
I'm disappointed with you Shai for recommending bad practices (even with the warning), you know better :(
@George Don't do this, use cells or structs.
@Amro you are right. shame on me!! I was about to edit my answer to suggest a practicle method to avoid evil eval...
thanks @Shai for listing an alternative to the often misused eval, much better now :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.