필터 지우기
필터 지우기

How to graph the data in Matlab that includes the headers, by importing the data using readtable?

조회 수: 13 (최근 30일)
I have a data table in Excel, with two headers, one horizontal where the months are, and another vertical where the years are. Both headers are of character type. How to graph the data in Matlab that includes the headers, by importing the data using readtable?

채택된 답변

Drew
Drew 2024년 1월 22일
편집: Drew 2024년 1월 22일
This answer is updated to use the data attached in the comment.
% Take the default reading of the table
t=readtable("US_PREG_03.xlsx");
% Get raw data as numbers
rawdata = table2array(t(:,2:end)); % Table to cellarray
rawdata = strrep(rawdata, ',', ''); % Remove commas
rawdata = str2double(rawdata); % Convert to numeric
% Assuming 'rawdata' is a 104x12 matrix with oil production data
% with rows representing years 1920 through 2023
% and columns representing months January through December.
%% Create monthly production line chart
% Create a time vector from January 1920 to December 2023
timeVector = datetime(1920, 1, 1):calmonths(1):datetime(2023, 12, 1);
% Reshape rawdata to be a single vector
productionVector = reshape(rawdata', [], 1);
% Plot the line graph
figure;
plot(timeVector, productionVector);
title('Monthly Oil Production (1920-2023)');
xlabel('Year');
ylabel('Oil Production');
grid on;
%% Alternate view: A stacked bar chart
% Assuming 'rawdata' is the same 104x12 matrix as before.
% Generate a vector for the years
years = 1920:2023;
% Create the stacked bar chart
figure(2);
bar(years, rawdata, 'stacked');
title('Yearly Oil Production (1920-2023)');
subtitle('with Monthly Breakdown');
xlabel('Year');
ylabel('Oil Production');
legend('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec', ...
'Location', 'BestOutside');
grid on;
Original answer, before the comment with more details and the data attached:
What type of graph do you want to make? Here are examples of a heatmap and a line graph
% Create some random data for the example
months = {'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'};
years = {'2019', '2020', '2021', '2022'};
data = rand(4, 12) * 100; % Random values between 0 and 100
% Create a table with the data
dataTable = array2table(data, 'RowNames', years, 'VariableNames', months);
% Write the table to an Excel file
writetable(dataTable, 'data.xlsx', 'WriteRowNames', true);
%%
% Import the data from the Excel file
dataTable = readtable('data.xlsx', 'ReadRowNames', true);
% Convert the table to an array for plotting
data = table2array(dataTable);
% Get the row and column headers
years = dataTable.Properties.RowNames;
months = dataTable.Properties.VariableNames;
% Plot a heatmap of the data
figure;
heatmap(months, years, data);
title('Heatmap of Monthly Data');
% Plot a line graph with a line for each year
figure;
plot(data', 'LineWidth', 2); % Transpose data to have months on the x-axis
legend(years, 'Location', 'northeastoutside');
xlabel('Month');
ylabel('Value');
xticks(1:length(months));
xticklabels(months);
title('Monthly Data Over Years');
If this answer helps you, please remember to accept the answer
  댓글 수: 3
Gabriel
Gabriel 2024년 1월 22일
@Drew, wonderful, thanks friend.. those ticks to convert data were incredible, blessings for your effort and help.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by