MATLAB: How to import data for multiple files using for loop data importloop I named files as 1.data,2.data,…. I want to import these data,process the data using functions,plot the processed data. my code is When i give nname in command window it is showing 1.data. but i am getting error as Best Answer Here is a portion of code I use to read multiple files in cases like this:for r=1:6 % Number of filesmyfilename= sprintf('%i.data',r); %file names as 1.data, 2.data and so on.filename = fullfile('C:\','Users','User','MATLAB',myfilename); % Put your file path herefid = fopen(filename,'r'); % Open the fileMydata = textscan(fid,'%f %f');% Assuming you have 2 columns of floating point data. Update as per your file accordingly.fid = fclose(fid); % CLose the file%%Delete temporary variables from workspace%%Process the data%%Plot the dataendHope this helps!! Related SolutionsMATLAB: Reading/writing in data from set of text files in loop You can use 'sprintf' command in a loop to achieve this. Something like this:Mydata = zeros(100000, 10);for i=1:1 % Number of test1->test2 set of files for j=1:10 % Number of test 1.001->test 1.002 set of files k=i+(j/1000); myfilename= sprintf('test %f.txt',k);filename = fullfile('C:\','Users','me','Documents','MATLAB',myfilename);fid = fopen(filename, 'r');Mydata(:,j) = textscan(fid, '%f', sample_stopcount, 'Headerlines', lopoff);%use the index i to create multiple 'Mydata' when reading test1->test2 etc.fid = fclose(fid); endend MATLAB: Importing data from multiple text files and plot on one figure Since you have not shared a sample text file, I assumed a few things. You can tweak this code a bit as per your files and that should work.for ii=1:24 % Read one file at a timemyfilename= sprintf('%f.txt',ii);% Assuming you have file names as 1.txt,2.txt and so on.filename = fullfile('D:\','STUDY','IIST Summer Internship May July 2019','Week 3','8th June','Files',myfilename);fid = fopen(filename, 'r'); % Open the fileMydata= fscanf(fid, '%f'); % Read the file assuming there are no text headersfid = fclose(fid); % CLose the fileplot(Mydata(:,1),Mydata(:,2)) % Plot the datahold on % Hold the plotend % End and repeat for all files Related QuestionExtracting data from multiple files with header.Importing txt files and using loopsI aam having error when i am trying to load csv file and i am having error like this which i mentioned so please help me someoneHow to import large text file data in to workspaceHow to show path of file in GUI editboxHow to import from a csv with a different file name
Best Answer