I have two csv files with only one colum each. I want to extract only the first number (measurement counter) and Sensor output last (V), i dont care about the water temp. The second file has less measurements (rows) than the first, so i need to chop out some measurements (rows). Preferably i want to start from a specific row, for example row 3-9 in CSVfile1 and row 4-8 in CSVfile2. Can anyone explain how i could do this, if its even possible?
I also need to plot the two tables/arrays in a same graph.
Best regards,
-Ben

댓글 수: 4

Those look to me as if they have 3 columns, not one ??
Unfortunately not. Picologger (the adc program) types them all in the same colum.
Can you attach a file with a few sample lines?
Of course.
I use excel to open them.

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

 채택된 답변

Mathieu NOE
Mathieu NOE 2021년 5월 3일

0 개 추천

hello
my quick suggestion to the quick question
fid=fopen('cycle1.csv');
e=textscan(fid,'%s','headerlines',1);
e_splitted = split(e{1},',');
outdata = cellfun(@str2num,strrep(e_splitted, '"', '')); % remove double quotes & convert string to num
% get my Counter and Sensor_Output data
start_row = 3;
stop_row = 9;
Counter_Output = outdata(start_row:stop_row,1);
Sensor_Output = outdata(start_row:stop_row,2);

댓글 수: 6

Good answer but it gets problematic when i plot. i need to plot to different vectors, and matlab cant seem to do it.
fid=fopen('cycle1.csv');
fid2=fopen('cycle2.csv');
e=textscan(fid,'%s','headerlines',1);
e2=textscan(fid2,'%s','headerlines',1);
e_splitted = split(e{1},',');
e_splitted2 = split(e2{1},',');
outdata = cellfun(@str2num,strrep(e_splitted, '"', ''));% remove double quotes & convert string to num
outdata2 = cellfun(@str2num,strrep(e_splitted2, '"', ''));
% get my Counter and Sensor_Output data
start_row = 3;
stop_row = 9;
start_row2 = 4;
stop_row2 = 8;
Counter_Output = outdata(start_row:stop_row,1);
Sensor_Output = outdata(start_row:stop_row,2);
Sensor_Output2 = outdata2(start_row2:stop_row2,2);
plot(Counter_Output,Sensor_Output);
hold on;
plot(Counter_Output,Sensor_Output2);
hold off;
This is your code modified and implemented with one more csv file. The problem start with the plot, the vectors arent equivalent. I dont know if this is possible to plot.
hello
if you want plot to operate , the x, and y vectors must be same length
so you have to make sure that Sensor_Output2 is associated with for example Counter_Output2 that is computed with the same start and stop indexes :
Counter_Output2 = outdata(start_row2:stop_row2,1);
now the last line of your code should be :
plot(Counter_Output2,Sensor_Output2);
a even more elegant way would be to vcreate a function and pass the arguments (filename, start and stop indexes ,...) so you're keeping the main section of the code clean and compact
Full code update :
fid=fopen('cycle1.csv');
fid2=fopen('cycle2.csv');
e=textscan(fid,'%s','headerlines',1);
e2=textscan(fid2,'%s','headerlines',1);
e_splitted = split(e{1},',');
e_splitted2 = split(e2{1},',');
outdata = cellfun(@str2num,strrep(e_splitted, '"', ''));% remove double quotes & convert string to num
outdata2 = cellfun(@str2num,strrep(e_splitted2, '"', ''));
% get my Counter and Sensor_Output data
start_row = 3;
stop_row = 9;
start_row2 = 4;
stop_row2 = 8;
Counter_Output = outdata(start_row:stop_row,1);
Sensor_Output = outdata(start_row:stop_row,2);
Counter_Output2 = outdata2(start_row2:stop_row2,1);
Sensor_Output2 = outdata2(start_row2:stop_row2,2);
plot(Counter_Output,Sensor_Output);
hold on;
plot(Counter_Output2,Sensor_Output2);
hold off;
better coding example - you can add as many files as you want without making the main code too unreadable (and less robust)
%% main code in 3 lines !!
[Counter_Output,Sensor_Output] = my_function('cycle1.csv',3,9);
[Counter_Output2,Sensor_Output2] = my_function('cycle2.csv',4,8);
plot(Counter_Output,Sensor_Output,'b',Counter_Output2,Sensor_Output2,'dr');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [Counter_Output,Sensor_Output] = my_function(filename,start_row,stop_row)
fid=fopen(filename);
e=textscan(fid,'%s','headerlines',1);
e_splitted = split(e{1},',');
outdata = cellfun(@str2num,strrep(e_splitted, '"', '')); % remove double quotes & convert string to num
% get my Counter and Sensor_Output data
Counter_Output = outdata(start_row:stop_row,1);
Sensor_Output = outdata(start_row:stop_row,2);
end
You are a legend my friend.
Thanks!
Mathieu NOE
Mathieu NOE 2021년 5월 4일
my pleasure

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

추가 답변 (2개)

EmirBeg
EmirBeg 2021년 5월 3일
편집: EmirBeg 2021년 5월 3일

1 개 추천

Data1 = readtable('cycle1.csv');
Data2 = readtable('cycle2.csv');
t1 = table2array(Data1(:,1));
SensorOutput1 = table2array(Data1(:,2));
t2 = table2array(Data2(:,1));
SensorOutput2 = table2array(Data2(:,2));
SizeDiff = size(SensorOutput1,1)-size(SensorOutput2,1);
StartingRow = 1; %set the row that you want to start with
t1 = t1(StartingRow:size(t1,1)-SizeDiff);
SensorOutput1 = SensorOutput1(StartingRow:size(SensorOutput1,1) - SizeDiff);
plot(t1,SensorOutput1);
hold on;
plot(t2,SensorOutput2);
hold off;

댓글 수: 1

This is a bit cumbersome:
t1 = table2array(Data1(:,1));
SensorOutput1 = table2array(Data1(:,2));
t2 = table2array(Data2(:,1));
SensorOutput2 = table2array(Data2(:,2));
It would be easier just to use dynamic variable indexing:
t1 = Data1.(1);
SensorOutput1 = Data1.(2);
t2 = Data2.(1);
SensorOutput2 = Data2.(2);

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

Jeremy Hughes
Jeremy Hughes 2021년 5월 4일

0 개 추천

I'd do this:
opts = detectImportOptions('cycle1.csv','Delimiter',',','VariableNamingRule','preserve');
opts.SelectedVariableNames = opts.VariableNames(1:2);
opts.DataLines = [3 9];
T1 = readmatrix('cycle1.csv',opts);
opts.DataLines = [4 8];
T2 = readmatrix('cycle2.csv',opts);
I'll leave the plotting to you.

카테고리

도움말 센터File Exchange에서 App Building에 대해 자세히 알아보기

제품

릴리스

R2018b

질문:

2021년 5월 3일

댓글:

2021년 5월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by