1

我正在尝试使用 Matlab 读取 CSV 文件并绘制它。基本上,我想绘制时间与心率的关系。但是,当我使用textscan它将两列复制到一个中时,我不确定如何将其分开。我想将时间保存在变量中x,将心率保存在变量中y并绘制它们。

到目前为止,我已经这样做了:

clear, clc;
ftoread = 'data.csv';
fid = fopen(ftoread); %Reads the CSV file
data = textscan(fid,'%s%f'); % Read in a string and a double
fclose(fid); % close
for i=2:size(data{1,1},1)% checks for size of the csv file
    x = strnum(data);%i need to separate the time from BPM using the comma thing and string command
    %y = data{2};
end
plot(x,y);

在 Matlab 的data单元格中,这就是我所看到的:

'Time,BPM(HeartRate)'
'5:55:26,0'
'5:55:26,66'
'5:55:27,69'
'5:55:27,71'
'5:55:27,72'

等等

for循环中,我想将时间分开并将其保存在一个变量中,并将心率设置为另一个变量。

4

2 回答 2

1

你需要标题信息'Time,BPM(HeartRate)'吗?textscan有许多不错的功能(请参阅文档)。特别是,您要求它忽略固定数量的标题行,您可以告诉它使用什么分隔符(默认为空格):

data = textscan(fid,'%s%f','Headerlines',1,'Delimiter',',');

现在的输出data{1}将如下所示:

'5:55:26'
'5:55:26'
'5:55:27'
'5:55:27'
'5:55:27'

并且data{2}将是一个双值向量。

于 2014-02-04T20:32:46.347 回答
1

要回答如何将时间值读入一个变量以及将读数读入第二个变量的基本问题,请使用以下命令:

clear;
clc;
ftoread = 'hr.csv';
fid = fopen(ftoread); %OPENS the CSV file
data = textscan(fid,'%s%f','Headerlines',1,'Delimiter',','); %Reads the file
fclose(fid); % closes the file

x = datenum(data{1}); %Time variables moved to new variable called x
y = data{2}; % Readings moved to variable y

plot(x,y); % plot bp readings vs time

% Create xlabel
xlabel('Time of Reading');

% Create ylabel
ylabel('Blood Pressure Reading');

% Create title
title('Blood Pressure Readings vs Time');

您的数据目前看起来并不多,因为血压读数基本上都是在同一秒记录的。

要调整您的 x 值,使其相对于第一次读数而不是时间的开始,您可以假设 csv 条目按顺序排列:

clear, clc;
ftoread = 'hr.csv';
fid = fopen(ftoread); %Reads the CSV file
data = textscan(fid,'%s%f','Headerlines',1,'Delimiter',',');
fclose(fid); % close

x = datenum(data{1});
x = x - x(1); % Times relative to the first recorded time entry
y = data{2};

plot(x,y);

% Create xlabel
xlabel('Seconds since first reading');

% Create ylabel
ylabel('Blood Pressure Reading');

% Create title
title('Blood Pressure Readings vs Time');
于 2014-02-04T20:53:32.010 回答