- ‘readmatrix’:https://www.mathworks.com/help/matlab/ref/readmatrix.html
- ‘bar’:https://www.mathworks.com/help/matlab/ref/bar.html
- ‘cumsum’:https://www.mathworks.com/help/matlab/ref/cumsum.html
How can I count and plot
15 views (last 30 days)
Show older comments
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1379199/image.png)
If I have a excel file looks like this, I'd like to plot time vs total number of count as shown below.
y-axis is the number of counts, while x-axis is the range.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1379204/image.png)
0 Comments
Answers (1)
Jaswanth
on 5 Jan 2024
Hi,
I understand that you wish to extract data from an Excel file and display it in a chart format similar to the one you provided. To achieve this, you can import the data from Excel using the ‘readmatrix’ function and create a bar chart with the ‘bar’ function. If you need to calculate a running total over time, the ‘cumsum’ function can be utilized. It computes the cumulative sum of the elements, beginning from the start of the first array dimension with a size larger than 1.
Please go through the following MathWorks documentation to know more about the functions mentioned above:
Please go through the following example code showing how to plot from an Excel file using a bar chart in MATLAB.
% Assume the Excel file is named 'data.xlsx' and the data is in the first sheet
% The time data is in the first column and the count data is in the second column
% Read the table from the Excel file
data = readtable('data.xlsx');
% Extract the time data
time_data = data{:, 1};
% Extract the count data
count_data = data{:, 2};
% Replace empty cells or NaN values with zeros
count_data(isnan(count_data)) = 0;
% Calculate the cumulative total count
cumulative_count_data = cumsum(count_data);
% Create a bar chart
bar(time_data, cumulative_count_data);
% Set the labels and title
xlabel('Time');
ylabel('Cumulative Total Count');
title('Cumulative Count vs. Time from Excel Data');
Hope the above provided information is helpful.
Thanks.
0 Comments
See Also
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!