Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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 ... !

share|improve this question

2 Answers 2

up vote 2 down vote accepted

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

share|improve this answer
    
Update1: @KlausCPH the first line I had to modify it to get what I want .. like this: Indi_Results {idx,1} = eval('x') but I couldn't figure out how to modify the second line (and therefore, everyline that follows) because ... If I let it as it is, I get the following error message: Undefined function 'eval' for input arguments of type 'cell'. and if I modified it this way: eval(['x' '_5P_software1']) .. i Get this error message: Undefined function or variable 'x_5P_software1' .. (x is recognized as a single letter!) could you please help again?! –  Leo... Jan 21 '13 at 9:41
    
ok. It might be because your string in x is in a cell. Try and change your line Indi_All {idx,1} (1,1) to Indi_All {idx,1} {1,1}. If that not works, then find out what type x is and post it here. –  KlausCPH Jan 21 '13 at 10:09
    
Thanks a lot for the respond .. I checked it again and x was in a cell ... so I changed the code to: Indi_Results {idx,2} = prctile (eval([x{1,1} '_Sim_V_software1'])', (5)) ... and it worked perfectly ... thanks again –  Leo... Jan 21 '13 at 10:23

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.

share|improve this answer
    
I'm not sure the variables Come from files that Can be loaded. At least it's not state anywhere as far as I can see. –  KlausCPH Jan 19 '13 at 19:29
    
@KlausCPH: The OP specifically says "Mike results saved as..." –  nimrodm Jan 20 '13 at 18:34
    
True. However, earlier they are referred to as "variables". But no point in discussing this when OP just as we'll can clear this up :-). So Leo...: are your dataset originating from files with the mentioned names, or are they variables (maybe generated by some other script)? –  KlausCPH Jan 20 '13 at 19:53
    
Guys, thanks a lot for answering and Sorry a lot for being late to respond ... the previously mentioned data are already saved as variables .... (I already imported them from many excel files using the xlsread! However, it will be interesting to know if there is an easier way to import the data without repeating the code sequence for each file!) .. I will just try your solution and I'll then update ... –  Leo... Jan 21 '13 at 9:05
    
@ nimrodm Thanks a lot man .. I think your code will help me a lot to improve (and shorten another code -to import the raw data from the excel files-) however, I can't try it until tomorrow, as I must work today on something else :( –  Leo... Jan 21 '13 at 9:42

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.