Plotting a curve from 2 excel columns

3 views (last 30 days)
I have two imported columns from Excel, the first one contains the time in HH:MM:SS Format in which the values in the second column were saved. I want to plot the columns against each other and form a curve with the time on the x-axis and the values on the y-axis, but I only get a figure with lines. Is there any code line that I can use to reshape the plotted line into a curve?
  3 Comments
Utkarsh Belwal
Utkarsh Belwal on 31 Aug 2020
Please share your code or data points which you are trying to plot.
Utkarsh Belwal
Utkarsh Belwal on 31 Aug 2020
Thanks for sharing the code. I can't find anything wrong with the code so if possible can you share the Excel files that you are using, that would be really helpful.

Sign in to comment.

Accepted Answer

Utkarsh Belwal
Utkarsh Belwal on 1 Sep 2020
Thanks for sharing the tables.
After looking into the tables shared by you, I can see that the columns f1, f2 and f3 has two values in each cell separated by comma. In the code you are not separating these values before plotting, that is why your plots are not matching and giving error. I have used split function to separate these values and after splitting used the first colmumn for plotting, look into the documentation of split for more information. Below is your code with few modifications to plot frequencies correctly,
dimtime= H2BLSENSOR_MeanVoltDim.time; %Dimtime in [HH:MM:SS]
dimavg= sum(dim)/size(dim,1); %Average Value of dim in [V].
bll = H2_BLSENSOR_MeanVoltBll.Value; %bll variable column in [v]
blltime= H2_BLSENSOR_MeanVoltBll.time;%blltime in [HH:MM:SS]
bllavg=sum(bll)/size(bll,1); %Average Value of bll in [V]
timef1=H2_BLSENSOR_MeanVoltBll1.timef1; %Time Column for the 1st frequency interval.
timef2=H2_BLSENSOR_MeanVoltBll2.timef2; %Time Column for the 2nd frequency interval.
timef3=H2_BLSENSOR_MeanVoltBll3.timef3; %Time Column for the 3rd frequency interval.
f1=H2_BLSENSOR_MeanVoltBll1.f1; %1st frequency intervall
f2=H2_BLSENSOR_MeanVoltBll2.f2; %2nd frequency intervall
f3=H2_BLSENSOR_MeanVoltBll3.f3; %3rd frequency intervall
v1=H2_BLSENSOR_MeanVoltBll1.Value;%Voltage Value 1
v2=H2_BLSENSOR_MeanVoltBll2.Value;%Voltage Value 2
v3=H2_BLSENSOR_MeanVoltBll3.Value;%Voltage Value 3
nexttile
F1 = double(split(string(f1(1:end-1)) , ","));
F1 = F1(:,1);
plot(timef1(1:end-1),F1);
xticks([timef1(1) timef1(end-1)]);
yticks([F1(1) F1(end-1)]);
nexttile
F2 = double(split(string(f2(2:end-1)) , ","));
F2 = F2(:,1);
plot(timef2(2:end-1),F2);
xticks([timef2(2) timef2(end-1)]);
yticks([F2(2) F2(end-1)]);
nexttile
F3 = double(split(string(f3(1:end-1)) , ","));
F3 = F3(:,1);
plot(timef3(1:end-1),F3);
xticks([timef3(1) timef3(end-1)]);
yticks([F3(1) F3(end-1)])
nexttile
plot(dimtime,dim)
hold on
plot(xdim,[dimavg dimavg],'r-','LineWidth',1);
xticks([dimtime(1) dimtime(end)]);
title('H2BLSENSOR_ MeanVoltDim [V]');
grid on
hold off
I am getting this figure as an output:
Last plot is not coming because you have not shared H2_BLSENSOR_MeanVoltBll table.
DISCLAIMER: These are my own views and in no way depict those of MathWorks.
  4 Comments
Ramo Rafsel
Ramo Rafsel on 3 Sep 2020
Edited: Ramo Rafsel on 3 Sep 2020
Hi Utkarsh, I looked again but I didn't find any mistake in the imported columns. But I guess I will find that out somehow. Otherwise the code and you provided me works wonders.
Just a question since I am not sure if I understand what the split function does:
this line basically just converts the values from the columns to double ones right?
and regarding 1:end-1, it just means the first value until the next-to-last value right?
Thanks a lot in advance for your big support :)
F1 = double(split(string(f1(1:end-1)) , ","));
Utkarsh Belwal
Utkarsh Belwal on 3 Sep 2020
Edited: Utkarsh Belwal on 3 Sep 2020
Split function splits a string into multiple strings at delimiters. In this case the delimiter is ',' so first the code converts each cell to string so that we can split them and the double outside converts string back to double.
Yes you are right regarding 1:end-1, I used to it because the last cell has only 1 value so split function was giving error.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!