How to read multiple text files and plot data?

9 visualizaciones (últimos 30 días)
Radhika Kulkarni
Radhika Kulkarni el 5 de Ag. de 2020
Comentada: Mario Malic el 5 de Ag. de 2020
I have 6-7 different log files and I need to plot the data on a graph. Here is my code for retriving data from a single file and plotting it.
fid = fopen('North.txt');
C = textscan(fid, '%s');
endc = length(C{1});
posx = [str2double(C{1}{1})];
index = 1;
for v=13:12:endc
index = index + 1;
posx(index) = str2double(C{1}{v});
end
posy = [str2double(C{1}{2})];
index = 1;
for v=14:12:endc
index = index + 1;
posy(index) = str2double(C{1}{v});
end
posz = [str2double(C{1}{3})];
index = 1;
for v=15:12:endc
index = index + 1;
posz(index) = str2double(C{1}{v});
end
rotx = [str2double(C{1}{4})];
index = 1;
for v=16:12:endc
index = index + 1;
rotx(index) = str2double(C{1}{v});
end
roty = [str2double(C{1}{5})];
index = 1;
for v=17:12:endc
index = index + 1;
roty(index) = str2double(C{1}{v});
end
rotz = [str2double(C{1}{6})];
index = 1;
for v=18:12:endc
index = index + 1;
rotz(index) = str2double(C{1}{v});
end
eulx = [str2double(C{1}{7})];
index = 1;
for v=19:12:endc
index = index + 1;
eulx(index) = str2double(C{1}{v});
end
euly = [str2double(C{1}{8})];
index = 1;
for v=20:12:endc
index = index + 1;
euly(index) = str2double(C{1}{v});
end
eulz = [str2double(C{1}{9})];
index = 1;
for v=21:12:endc
index = index + 1;
eulz(index) = str2double(C{1}{v});
end
objx = [str2double(C{1}{10})];
index = 1;
for v=22:12:endc
index = index + 1;
objx(index) = str2double(C{1}{v});
end
objy = [str2double(C{1}{11})];
index = 1;
for v=23:12:endc
index = index + 1;
objy(index) = str2double(C{1}{v});
end
objz = [str2double(C{1}{12})];
index = 1;
for v=24:12:endc
index = index + 1;
objz(index) = str2double(C{1}{v});
end
figure(1);
plot3(posx, posy, posz);
figure(2);
plot3(rotx, roty, rotz);
figure(3);
plot3(eulx, euly, eulz);
figure(4);
plot3(objx, objy, objz);
Now What I want to do is read data from 5 other files(which have similar file structure as the first one) and plot the data in graphs. How do I read multiple files and plot the data?

Respuestas (1)

Mario Malic
Mario Malic el 5 de Ag. de 2020
Editada: Mario Malic el 5 de Ag. de 2020
Here's some code that you can read the data from the .txt files, I haven't seen yours so it might not work properly on it. Data_Ready returns your data as an Array, so you'll need to know what rows are for what.
%% You can use it as a function
function Data_Ready = Read_File(File_Name)
Counter = 1;
File_Name = 'results.txt'
FID = fopen(File_Name, 'rt');
tline = fgetl(FID);
File_Data{Counter} = tline;
while ischar(tline)
Counter = Counter+1;
tline = fgetl(FID);
File_Data{Counter} = tline;
end
fclose(FID);
File_Data = File_Data';
Wanted_Rows = 1:1:length(File_Data);
% Delete_Lines = [1:13 114 115 216 217, length(File_Data)]; If you need to delete some rows
Delete_Lines = [length(File_Data)]; % Last line of File_Data is -1 which denotes end of file so we remove it here
File_Data(Delete_Lines) = [];
File_Data_Split = split(File_Data(1:length(File_Data))); % Splits the cell
Data_Ready = cellfun(@str2num,File_Data_Split);
end
If you want to plot all five files on 4 graphs, then you can read each file and join arrays with data accordingly.
  2 comentarios
Radhika Kulkarni
Radhika Kulkarni el 5 de Ag. de 2020
Thanks. But my code already converts the data into array and plots the graph. But my code is for single file. What I want is to plot graphs of data from multiple files. So How do I do that?
Mario Malic
Mario Malic el 5 de Ag. de 2020
If you want to do it that way, then exactly as you did already, copy and paste it how many times you need. There's command called "hold on" so the plot doesn't get overwritten.

Iniciar sesión para comentar.

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by