简体   繁体   中英

How to add values of a .csv file to arrays in Matlab?

Several values for temperatures, voltage and time are stored in my .csv file.
My goal is to get those different values for the temperature, voltage and time from the .csv file into a matrix in Matlab. The saved data in the .csv file has the following form :

I have tried multiple commands and script, with the one below I came closets to achieving my goal.

S = fileread("example.csv");
S(S == '"') = [];                                                                               
fmt = '%f%f%f%f%f%f%f%f%f%f';
A = textscan(S, fmt, 'HeaderLines', 1, 'Delimiter', ',', 'CollectOutput', true);
Data = cell2mat(A);

How can I get the data in a matrix?


Here is the content of example.csv as text:

Time,"T4","T2","T3","T4","T5","T6","T7","T8","Voltage"
00:00:00,"36.934","23.625","23.080","14.022","14.007","22.374","22.094","19.495","0.748"
00:00:01,"37.033","23.611","23.088","14.022","14.026","22.371","22.095","19.474","0.757"
00:00:02,"37.095","23.596","23.066","14.022","14.008","22.371","22.091","19.422","0.747"
00:00:03,"37.180","23.575","23.070","14.027","14.019","22.368","22.092","19.385","0.741"
00:00:04,"37.286","23.557","23.067","14.020","14.020","22.361","22.081","19.352","0.740"
00:00:05,"37.377","23.542","23.057","14.024","14.024","22.350","22.074","19.308","0.712"

I think I solved the puzzle:

According to your comments, the Excel sheet should look as in the following image:
在此处输入图片说明

When saving the from Excel to CSV the text of example.csv is:

"Time,""T4"",""T2"",""T3"",""T4"",""T5"",""T6"",""T7"",""T8"",""Voltage"""
"00:00:00,""36.934"",""23.625"",""23.080"",""14.022"",""14.007"",""22.374"",""22.094"",""19.495"",""0.748"""
"00:00:01,""37.033"",""23.611"",""23.088"",""14.022"",""14.026"",""22.371"",""22.095"",""19.474"",""0.757"""
"00:00:02,""37.095"",""23.596"",""23.066"",""14.022"",""14.008"",""22.371"",""22.091"",""19.422"",""0.747"""
"00:00:03,""37.180"",""23.575"",""23.070"",""14.027"",""14.019"",""22.368"",""22.092"",""19.385"",""0.741"""
"00:00:04,""37.286"",""23.557"",""23.067"",""14.020"",""14.020"",""22.361"",""22.081"",""19.352"",""0.740"""
"00:00:05,""37.377"",""23.542"",""23.057"",""14.024"",""14.024"",""22.350"",""22.074"",""19.308"",""0.712"""

But it could also look like the following one:
在此处输入图片说明

When saving the from Excel to CSV the text of example.csv is:

Time  ,"""T1""","""T2""    ","""T3""    ","""T4""    ","""T5""    ","""T6""    ","""T7""    ","""T8""    ","""Voltage"""
00:00:00,"""36.934""","""23.625""","""23.080""","""14.022""","""14.007""","""22.374""","""22.094""","""19.495""","""0.748"""
00:00:01,"""37.033""","""23.611""","""23.088""","""14.022""","""14.026""","""22.371""","""22.095""","""19.474""","""0.757"""
00:00:02,"""37.095""","""23.596""","""23.066""","""14.022""","""14.008""","""22.371""","""22.091""","""19.422""","""0.747"""
00:00:03,"""37.180""","""23.575""","""23.070""","""14.027""","""14.019""","""22.368""","""22.092""","""19.385""","""0.741"""
00:00:04,"""37.286""","""23.557""","""23.067""","""14.020""","""14.020""","""22.361""","""22.081""","""19.352""","""0.740"""
00:00:05,"""37.377""","""23.542""","""23.057""","""14.024""","""14.024""","""22.350""","""22.074""","""19.308""","""0.712"""

You can read both using the following code sample:

S = fileread("example.csv");
S(S == '"') = [];
S(S == ' ') = []; %Remoove spaces (just in case...)
fmt = '%f:%f:%f,%f,%f,%f,%f,%f,%f,%f,%f,%f';
%A = textscan(S, fmt, 'HeaderLines', 1, 'Delimiter', ',', 'CollectOutput', true);
A = textscan(S, fmt, 'HeaderLines', 1, 'CollectOutput', true);
Data = cell2mat(A);

Result:

Data =

         0         0         0   36.9340   23.6250   23.0800   14.0220   14.0070   22.3740   22.0940   19.4950    0.7480
         0         0    1.0000   37.0330   23.6110   23.0880   14.0220   14.0260   22.3710   22.0950   19.4740    0.7570
         0         0    2.0000   37.0950   23.5960   23.0660   14.0220   14.0080   22.3710   22.0910   19.4220    0.7470
         0         0    3.0000   37.1800   23.5750   23.0700   14.0270   14.0190   22.3680   22.0920   19.3850    0.7410
         0         0    4.0000   37.2860   23.5570   23.0670   14.0200   14.0200   22.3610   22.0810   19.3520    0.7400
         0         0    5.0000   37.3770   23.5420   23.0570   14.0240   14.0240   22.3500   22.0740   19.3080    0.7120

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM