【发布时间】:2021-03-25 07:28:25
【问题描述】:
我有一个文本文件,我必须从中检索数据并绘制一个看起来像这样的图表。我想绘制一个图表,机器通过该图表具有 status="on"。我试图找到机器处于开启状态的索引。
我做了什么:
fid=fopen('power.txt');
line=fgetl(fid);
data=textscan(fid,'%d %f %s');
fclose(fid);
time=data{1};
power=data{2};
status=data{3};
status_on=strcmp(data{3},'on');
indices=find(status_on==0);
start_indices=indices+1;
%adding the first index where the status is on
start_indices=[1; start_indices];
%removing the last element as the last index will always have the value off
start_indices(end)=[];
end_indices=indices-1;
%Plotting Graph
plot(time,power,'-r');
xlabel('Time (s)');
ylabel('Power (W)');
title('Sonications over time');
这个图表只是给了我一个简单的情节,但我需要让我的图表看起来像这样。另外我一直在搜索标记的使用,是否需要编写特定的代码来获取这些标记或者是否有默认的 Matlab 函数?
【问题讨论】:
-
显然当
status的幂为OFF时,第二列(power)显示-1,所以在第二列中查找该值,所有不是-1的都是然后ON. -
这是一个很好的观点。但是我需要我的图表在它关闭的时候是空白的,并且只有在我需要帮助的时候才会有一条线。
-
您可以使用循环并通过另一个调用
plot添加标记,并在使用hold on添加新绘图时保留当前绘图。
标签: matlab plot indexing time graph