How to read multiple text files and plot data?
조회 수: 10 (최근 30일)
이전 댓글 표시
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?
댓글 수: 0
답변 (1개)
Mario Malic
2020년 8월 5일
편집: Mario Malic
2020년 8월 5일
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
Mario Malic
2020년 8월 5일
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!