Read multiple text files and save the data into matrix

Hi
I have multiple text files (for example I have attaced here 2 files). I want to read them, and save the data into matrix. But I only want the data above the first dotted line (text left side to : in first column, and other data after : to the second column). Secondly, I want to save the data which is between two dotted lines (----). Again, the data under Item is in first column, and corresponding 3rd column in second column. Please help me some how to do this.

 Respuesta aceptada

Ingrid
Ingrid el 8 de En. de 2016
if your file always has the same amount of lines you could do something like this (if not, you need to check where the ---- occurs)
for ii = 1:Nfiles
fid = fopen(fileNames{ii});
if fid == -1
error('Unable to open txtfile');
end
fileData1{ii} = textscan(fid,'%s %s','Delimiter',':','HeaderLines',1,21);
fileData2{ii} = textscan(fid,'%s %s %s','Delimter','\t','HeaderLines',2);
fclose(fid);
end

5 comentarios

Kanakaiah Jakkula
Kanakaiah Jakkula el 8 de En. de 2016
Editada: Kanakaiah Jakkula el 8 de En. de 2016
Sir
But how to specify the location of the files.
giving following error: ??? Undefined function or variable 'Nfiles'.
Error in ==> ReadTextFiles_0108_m1 at 2 for ii = 1:Nfiles best regards, Mekala
of course this is something that you have to provide, I do not know how many files you have, nor where they are located. You can look into the dir() function to give you a list of fileNames and use numel(fileNames) to obtain Nfiles (=number of files)
p.s. it is madam
Hello Madam,
My files D:\Mekala_Backupdata\Matlab2010\Filesfolder\Try2. But the the number of text files will vary. I use the following code
clc
clear all
close all
FileList=dir('D:\Mekala_Backupdata\Matlab2010\Filesfolder\Try2/');
j=1;
for i=3:1:(size(FileList)) %%read all files from folder of specified dir
FileName{j}=FileList(i).name;
j=j+1;
end
for ii = 1:j
fid = fopen(fileNames{ii});
if fid == -1
error('Unable to open txtfile');
end
fileData1{ii} = textscan(fid,'%s %s','Delimiter',':','HeaderLines',1,21);
fileData2{ii} = textscan(fid,'%s %s %s','Delimter','\t','HeaderLines',2);
fclose(fid);
end
But it giving me below error:
??? Undefined variable "fileNames" or class "fileNames".
Error in ==> ReadTextFiles_0108_m1 at 12 fid = fopen(fileNames{ii});
Kindly help Madam
Kindly help
Change
FileName{j}=FileList(i).name;
to
FileNames{j}=FileList(i).name;

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Preguntada:

el 8 de En. de 2016

Comentada:

el 15 de En. de 2016

Community Treasure Hunt

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

Start Hunting!

Translated by