How to graph the data in Matlab that includes the headers, by importing the data using readtable?
19 次查看(过去 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?
0 个评论
采纳的回答
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Distribution Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!