Input String as Array Variable_Urgent help

2 views (last 30 days)
Juzer
Juzer on 7 May 2014
Commented: Geoff Hayes on 7 May 2014
% I give the file name as user input from command line as:
<<< main_script(115)
I have many file names such as Expressions_114, Expressions_112, Expressions_110..etc. So I only want to change the number:112,113,114,115...for my convenience
% My function is below:
main_script(y)
format long
load (['Expressions_' num2str(y) '.txt']);
% this will load the file: Expressios_115.txt
% Now Reading values columnwise from the file and storing it in an cell array
for i=1:5
Ring{:,i}=(['Expressions_' num2str(y)])(:,i);
end
% Here I get an error at (:,i) saying that Parse error at '(': Usage might be invalid matlab syntax
can somehow help me with this.
thanks
  1 Comment
Geoff Hayes
Geoff Hayes on 7 May 2014
Please edit the above code so that it is readable (highlight the code and press the {}Code button).

Sign in to comment.

Answers (1)

Geoff Hayes
Geoff Hayes on 7 May 2014
Juzer - your comments above seem to indicate that you are reading in some data from a text file and then trying to process the data (that you just read) column wise. I guess that you are assuming there are five columns in your data set. Your line:
load (['Expressions_' num2str(y) '.txt']);
will load the data from the file into a local variable of name Expressions_115 (here I'm assuming that y==115). You are then trying to rebuild that variable name on each iteration of your loop and trying to access the ith column. But what you have built is just a string and not a variable that you can access any data from. What you can do instead is build a command string, and then evaluate it at each iteration:
% this will build the prefix to the command string: 'Expressions_115(:,'
% everything you need to access all row elements from the variable just loaded
cmdStrPrefix = ['Expressions_' num2str(y) '(:,']
% we are assuming that there are only five columns in Expressions_115
for i=1:5
% this builds the complete command string to evaluate the ith column of the var
% for i==1, this would be 'Expressions_115(:,1)'
cmdStr = [cmdStrPrefix num2str(i) ')'];
% we then evaluate this command string to get the ith column of the var
Ring{:,i}=eval(cmdStr);
end
Try the above to see what happens. In order to avoid problems with the iterations (in case there are not five columns in the data), you could repeat the command string style to get:
% build the command string to get the number of columns in the variable
% the string will look like 'size(Expressions_115,2)'
cmdStr = ['size(Expressions_' num2str(y) ',2)'];
% evaluate the command and get the number of columns, n (use this to iterate over
% in the above for loop)
n = eval(cmdStr);

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!