Reading numeric values from csv file

Hi there. I am trying to read numeric values on matlab from a csv file that is created using PuTTY. I want to read data in realtime from row 5 going onwards and column 3 specifically. The data is being captured in realtime. I have tried a using a matlab code but I can't seem to read in the numeric values. I also want to store the values displayed into an array for later use. Please kindly assist

5 commentaires

Jan
Jan le 7 Juin 2022
Please post code as text, not as screenshot. Then it is much easier to re-use it in an answer.
"I can't seem to read" is not a useful explanation of what you observe. We cannot reproduce your setup, so it would be useful to tell us, what happens.
How does PuTTY create CSV files? Is the file still opened by PuTTY? Then you cannot access the contents usually.
[MOVED from section for answers] Lola wrote:
I apologise for the lack of information. PuTTY is open while running MATLAB code. I am trying to observe data in realtime from a sensor connected to arduino board. Through serial communication PuTTY displays values observed from the sensor. Basically PuTTY is replacing the Arduino IDE serial monitor.
Below is the code I used in MATLAB:
for i = 1:10000
filename = 'orange.csv';
x = csvread(filename,5,2)
pause(1);
end
The errors matlab shows are:
Error using dlmread
Number of HeaderColumns is greater than number of columns in file
@Lola: Usually Matlab is used to read the serial data directly, such that the indirections over PuTTY and a file are not necessary.
The start of the file looks strange:
=~=~=~=~=~=~=~=~=~=~=~= PuTTY log 2022.06.07 18:39:20 =~=~=~=~=~=~=~=~=~=~=~=
IR= 627 BPM= 0 Avg BPM= 0
626 BPM= 0 Avg BPM= 0
IR= 626 BPM= 0 Avg BPM= 0
0 Avg BPM= 0
IR= 623 BPM= 0 Avg BPM= 0
IR= 623 BPM= 0 Avg BPM= 0
IR= 626 BPM= 0 Avg BPM= 0
IR= 626 BPM= 0 Avg BPM= 0
IR= 630 BPM= 0 Avg BPM= 0
csvread requires a file with a strict layout.
Lola
Lola le 8 Juin 2022
Yes I'm not sure why the cells are getting mixed up. The reason I'm using PuTTY is because for now I'm reading sensor values from I2C connections. However the aim will be to read data from the receiving end of an nRF24l01 module. So I'm not able to read from a specific pin on arduino.
Walter Roberson
Walter Roberson le 8 Juin 2022
csvread can skip text header lines, and it can skip a fixed number of columns that can include text. But all entries after that on the line must be purely numeric. The only values that csvread would be able to handle in that file are the BPM (all of which happen to be 0 in the sample)

Connectez-vous pour commenter.

 Réponse acceptée

Chaitan Divagi
Chaitan Divagi le 8 Juin 2022
Hello Lola,
I understand that you want to read the numeric values from specific column from the csv file in realtime.
You can perform this task using MATLAB functions in the following manner:
data = readtable('<CSV-file>.csv','NumHeaderLines',5); % use readtable instead of csvread and 'NumHeaderLines', 5 skips the 5 rows from the table
col_vec = data(:, 3); % reads the 3rd colum from the table and stores in the col_vec variable
I hope that the information provided helps in resolving your query.
Thanks,
Chaitan,

3 commentaires

Lola
Lola le 10 Juin 2022
Modifié(e) : Lola le 10 Juin 2022
Hi. This worked. Thank you so much! I was able to read data from the columns specified. I would like to write those values (in col_vec) into a different excel file. Below is the code I tried.
xlswrite('orangedata.xls',col_vec,'Temperature','D1');
I get the error using xlswrite Input data must be a numeric,cell,or logical array.
I would also like to plot the values. Please assist
data = readtable('<CSV-file>.csv','NumHeaderLines',5); % use readtable instead of csvread and 'NumHeaderLines', 5 skips the 5 rows from the table
col_vec = data{:, 3} ; % reads the 3rd colum from the table and stores in the col_vec variable
Lola
Lola le 11 Juin 2022
Thank you that works perfectly

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by