Leo...
Leo...

Reputation: 321

Construct a variable name from two strings values in a loop function

I am a beginner in MATLAB and I hope that I will find the help (again :) ) in this nice website. It is about model prediction results for a series of individuals. therefore, I have a series of variables (containing the basic results of each Individual) and which their nomenclature differ only in the first part ..e,g:

Mike results saved as: Mike_Sim_V_software1 Adam results saved as: Adam_Sim_V_sofwtare1 Sarah results saved as: Sarah_Sim_V_sofwtare1 and so on ...

I already have the list of names ['Mike','Adam','Sarah', etc.] and the ID (%saved in the variable named: idx)

and Because I want to excute a series of calculations which is similar for all the above mentioned variables (the results variables), and as my variables' names differ only in their first part, I thought of writing a loop function like this:

for idx=1:80 
x= Indi_All {idx,1} (1,1) 

% idx: is the Individual IDs ... 80 is the total number of individuals
% Indi_All is a cell array containing the basic info, IDs and demographics.. 
here x will get the initial part of the variables names e.g. 'Mike' or 'Adam'

Indi_Results {idx,1} = x
Indi_Results {idx,2} = prctile (x_Sim_V_software1', (5))
Indi_Results {idx,2} = x_5P_software1'
Indi_Results {idx,3} = prctile (x_Sim_V_software1', (10))
Indi_Results {idx,3} = x_10P_software1'
Indi_Results {idx,4} = prctile (x_Sim_V_software1', (25))
Indi_Results {idx,4} = x_25P_software1'
Indi_Results {idx,5} = prctile (x_Sim_V_software1', [50])
Indi_Results {idx,5} = x_Median_software1'
Indi_Results {idx,6} = prctile (x_Sim_V_software1', (75))
Indi_Results {idx,6} = x_75P_software1'
Indi_Results {idx,7} = prctile (x_Sim_V_software1', (90))
Indi_Results {idx,7} = x_90P_software1'
Indi_Results {idx,8} = prctile (x_Sim_V_software1', [95])
Indi_Results {idx,8} = x_95P_software1'


% the code and calculations go even more and more ...

end 

so as the experts can see ... the x will be recognized in the code (right columns) as 'x' and not as I actually want it to mean, namely the variable x which contains a string value like: 'Mike' ..

My question is:

1) Can I solve this? Is there any function to construct the names of the variables from two different strings ?! so maybe something similar to num2str but to add string to string! In other words, to add the changing part 'Mike','Adam', etc. to the unchanging part '_Sim_V_software1' to get a variable which I can use in the loop function?!

I don't know... I have the feeling that my question is silly, but somehow I spent a lot of time for this and it is not working the way I want it to be! hope that this has nothing to do with the fact that my mind already knows something about the weekend going to start :)

I would be glad to hear something from you and thanks a lot in advance ... !

Upvotes: 1

Views: 601

Answers (2)

nimrodm
nimrodm

Reputation: 23799

Using eval is possible but probably not necessary.

Instead, I suggest you read the files into a cell array. Then manipulate them using by indexing into the array. For example,

fn = {'Mike','Adam','Sarah'};


for i=1:length(fn)
  % use the functional form of the load() function
  % to get the time series into t{i} regardless of the filename.

  t{i} = load([fn '_Sim_V_software1 ']);

  res(i, :) =  prctile (t{i}, [5 10 25 50 75 90 95]);
end

In the above example, you don't really need a cell array for t{}, but I'm assuming you'll want to do more analysis on the time series.

Upvotes: 1

KlausCPH
KlausCPH

Reputation: 1835

It is quite simple actually. You need to use the eval() function like this:

Indi_Results {idx,1} = eval(x)
Indi_Results {idx,2} = prctile (eval([x '_Sim_V_software1'])', (5))
Indi_Results {idx,2} = eval([x '_5P_software1'])'
...

For string concatenation you can use [str1 str2] as also shown above.

Regards

Upvotes: 2

Related Questions