Prev: unsupervised image segmentation..need help
Next: A question about Yen's K Shortest Paths algorithm in Matlab
From: ade77 on 28 May 2010 13:59 Randi, Copy the code below and run: ********************************************************************* %put all the files in one folder,and put anywhere on your computer clear,clc PathName = uigetdir; %this will let you navigate to the folder dirOutput = dir(fullfile(PathName,'*.txt')); % I assume all your file of interest are '.txt' FileNames = {dirOutput.name}'; %this will put all your files in cell array L = size(FileNames,1); %total number of files in the folder new_array = zeros(L,2); no_of_xvalues = 2 ; %in your case you want to make this 20 New_Data_Folder = 'Adjusted Data'; %create folder to put processed data mkdir(PathName,New_Data_Folder); %let us put the folder inside original folder for i = 1:no_of_xvalues xvalue = inputdlg('Type your x value', 'X VALUE TO USE'); xvalue = xvalue{:}; new_filename = xvalue; new_filename = fullfile(PathName,New_Data_Folder,new_filename); for k = 1:L data = importdata(fullfile(PathName,FileNames{k})); name_file = strtok(FileNames{k},'.'); %name of file without ext col_1 = str2num(name_file); %str2double is faster col_2 = data(data(:,1)== str2double(xvalue),2); % corresponding col2 from data new_array(k,:) = [col_1 col_2]; end dlmwrite([new_filename '.txt'], new_array); %text file xlswrite([new_filename '.xls'],new_array); %excel file end ********************************************************************* The above code will work for you, you can ignore the previous code you have written to calculate the new data. Copy the code to a new script file and run it Take Note: First put all the files in a folder on any directory on your computer 1. My assumption - The first column of each file, is unique, that is, it does not occur twice in the same file, like the 0.20, 0.30 only occur once in the first column. 2. When you run the code it will prompt you for the x values, so you type say 0.20, it will prompt you agian, then type say 0.30 etc. I have done it for only 2 times, in your own case it will be 20 times. but run it for 2, see the result, then do it for 20 3. The first for loop is where the prompt to enter x values, if you like, (which is acually better), you should just write the x values in vector form, like : x_values = [0.20 0.30 0.50.....], then you have to modify the first for loop. 4. The program will create both text file and excel file. 5. Since you are new to MATLAB, try and go thorugh the lines one after the other, look at the help file, if you do not understand any part, post back to get the info. 6. This is not the most optimal solution to the problem, but it is reasonably good Any problems, post back to get help. Good luck. |