csv file import with exponential notation

조회 수: 7 (최근 30일)
Thomas Unterholzner
Thomas Unterholzner 2020년 12월 29일
댓글: Thomas Unterholzner 2020년 12월 29일
Hello,
I importet data from a sensor reading and wanted to plot them (force over time)
as the force data contained commata I had to convert them, but I still have a wrong graph as the exponential notation is not 'detected'.
This is my script:
%%read txt file with force measurement, save datevec
Data = readtable('CSV-file.csv','TextType','string'); %add name of csv file (readtable)
waveform = table2array(Data(:,1)); %table to cell time vector
Y = table2array(Data(:,2)); %save force in waveform
Y([1,2,3,4,5],:) = []; %delete rows 1-5 force vector
waveform([1,2,3,4,5],:) = []; %delete rows 1-5 time vector
Data2 = strrep(Y, ',', '.');
Timevector = datetime (waveform ,'InputFormat','dd.MM.yyyy HH:mm:ss,SSSSS');
Data21 = categorical(Data2(:,1));
plot (Timevector, Data21,'b*');
yyy HH:mm:ss,SSSSS');
Data21 = categorical(Data2(:,1));
plot (Timevector, Data21,'b*');
my Data (.csv):
and this is my current plot vs. the actual plot:
is there a way to format the string, so that the exponential notation is right?
  댓글 수: 1
Ameer Hamza
Ameer Hamza 2020년 12월 29일
편집: Ameer Hamza 2020년 12월 29일
Can you attach some sample datapoints from as a csv file?

댓글을 달려면 로그인하십시오.

채택된 답변

Cris LaPierre
Cris LaPierre 2020년 12월 29일
Here's how I would do it.
%%read txt file with force measurement, save datevec
opts = detectImportOptions("CSV-file.csv","NumHeaderLines",6);
opts = setvartype(opts,"DateTime","datetime");
opts = setvaropts(opts,"DateTime","InputFormat",'dd.MM.yyyy HH:mm:ss,SSSSS');
opts = setvartype(opts,"Y_Chan_3_","string");
Data = readtable('CSV-file.csv',opts);
Warning: Column headers from the file were modified to make them valid MATLAB identifiers before creating variable names for the table. The original column headers are saved in the VariableDescriptions property.
Set 'VariableNamingRule' to 'preserve' to use the original column headers as table variable names.
% specifying the decimal separator didn't help because of the formatting.
% This replaces it with a period. This may not be necessary depending on your regional settings.
Data.Y_Chan_3_ = strrep(Data.Y_Chan_3_,',','.');
Data.Y_Chan_3_ = str2double(Data.Y_Chan_3_)
Data = 3507x2 table
DateTime Y_Chan_3_ _________________________ _________ 21.12.2020 19:21:32,15037 -0.012731 21.12.2020 19:21:32,25045 -0.012731 21.12.2020 19:21:32,35054 -0.025462 21.12.2020 19:21:32,45062 -0.012731 21.12.2020 19:21:32,55071 -0.012731 21.12.2020 19:21:32,65079 -0.012731 21.12.2020 19:21:32,75088 -0.025462 21.12.2020 19:21:32,85096 -0.025462 21.12.2020 19:21:32,95105 -0.025462 21.12.2020 19:21:33,05113 -0.025462 21.12.2020 19:21:33,15122 -0.012731 21.12.2020 19:21:33,25130 -0.025462 21.12.2020 19:21:33,35139 -0.025462 21.12.2020 19:21:33,45147 -0.038193 21.12.2020 19:21:33,55156 -0.11458 21.12.2020 19:21:33,65164 -0.29282

추가 답변 (1개)

Walter Roberson
Walter Roberson 2020년 12월 29일
Use the decimalseparator option for readtable
https://www.mathworks.com/help/matlab/ref/detectimportoptions.html#d122e302928
I think it became available as of R2020a but that would have to be checked
  댓글 수: 1
Walter Roberson
Walter Roberson 2020년 12월 29일
You might need Prefix and Suffix to get rid of quote marks

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 LaTeX에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by