필터 지우기
필터 지우기

How to get a plot out of a .csv data from a sensor?

조회 수: 70 (최근 30일)
Mathias Braun
Mathias Braun 2023년 12월 10일
답변: Mathias Braun 2023년 12월 15일
Hello Community,
I have a problem with creating a plot from the .csv-data which I created by a distance sensor. I want to have the measure time in the x-axis and the distance on the y-axis. Could anyone help me for coding?
Thank you for helping me.

채택된 답변

Image Analyst
Image Analyst 2023년 12월 10일
Try this:
fileName = 'Sample_0012023-11-30 15.05.48.343_A0000.csv'
fileName = 'Sample_0012023-11-30 15.05.48.343_A0000.csv'
t = readtable(fileName)
t = 30002×3 table
Var1 Var2 Var3 ____________________ ______ __________ NaT NaN {' ' } NaT NaN {0×0 char} 30-Nov-2023 15:05:48 7.0486 {0×0 char} 30-Nov-2023 15:05:48 7.0486 {0×0 char} 30-Nov-2023 15:05:48 7.0486 {0×0 char} 30-Nov-2023 15:05:48 7.0486 {0×0 char} 30-Nov-2023 15:05:48 7.0486 {0×0 char} 30-Nov-2023 15:05:48 7.0486 {0×0 char} 30-Nov-2023 15:05:48 7.0486 {0×0 char} 30-Nov-2023 15:05:48 7.0486 {0×0 char} 30-Nov-2023 15:05:48 7.0486 {0×0 char} 30-Nov-2023 15:05:48 7.0486 {0×0 char} 30-Nov-2023 15:05:48 7.0486 {0×0 char} 30-Nov-2023 15:05:48 7.0486 {0×0 char} 30-Nov-2023 15:05:48 7.0486 {0×0 char} 30-Nov-2023 15:05:48 7.0486 {0×0 char}
times = t.Var1;
distances = t.Var2;
plot(times, distances, 'b-');
grid on;
xlabel('Time');
ylabel('Distance');
title('Distance vs. Time');

추가 답변 (3개)

akshatsood
akshatsood 2023년 12월 10일
편집: akshatsood 2023년 12월 10일
I understand that you are encountering difficulties in creating a plot from the CSV data generated by a distance sensor. To plot "time vs distance plot", please refer to the following steps
  1. Load the CSV data into a "table" datatype using "readtable" function.
  2. Extract the values for time and distance from the "data" table
  3. Use "scatter" function to plot time vs distance
Here is the code snippet for your reference
% load CSV data to table
data = readtable("Sample_0012023-11-30 15.05.48.343_A0000.csv");
% read time and distance from the table
time = data(:,1).Var1;
distance = data(:,2).Var2;
% scatter plot for time vs distance
scatter(time, distance);
% add annotations
xlabel('Time');
ylabel('Distance');
title('Distance vs. Time');
axis tight;
Have a look at the following references for functions used and gain a better understanding
Please let me know if you have any further questions.
I hope this helps.

Alexander
Alexander 2023년 12월 10일
My old-fashioned solution, not "fine arts", but works.
clear; close all;
Lines = char(zeros(30000,40)); % Could be any number as long as larger than expected and fitting in memory
LC = 1; % Loop counter
fid = fopen('Sample_0012023-11-30 15.05.48.343_A0000.csv');
Header = fgetl(fid)
DAQstart = fgetl(fid)
ColumnName = fgetl(fid)
while ~feof(fid)
Lines(LC,:) = fgets(fid);
LC = LC + 1;
end
fclose(fid);
Data = str2double(string(char(Lines(:,31:end)))); % The meassured data
D = Lines(:,1:19); % The Date
FS = str2double(string(Lines(:,20:26)))/24/60/60; % Fractions of Seconds
Tnum = datenum(D(1:end,:),'yyyy-mm-dd HH:MM:SS')+FS; % The date in numbers
Tstart = (Tnum - Tnum(1))*24*60*60; % The timeline beginning at 0
%plot(Tnum, Data)
plot(Tstart, Data); xlabel('t [s]'); ylabel('Distance'); title(DAQstart)

Mathias Braun
Mathias Braun 2023년 12월 15일
Thank you guys for these great answers ;) I really appreciate your help.

카테고리

Help CenterFile Exchange에서 Develop Apps Using App Designer에 대해 자세히 알아보기

태그

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by