plot graph from cell array

51 views (last 30 days)
C.G.
C.G. on 11 Jan 2021
Edited: Adam Danz on 13 Jan 2021
I have loaded 15 excel files into matlab and saved them all as a 15x1 cell.
My aim is to create 15 graphs, showing the contents of each row of the cell array against tim in a subplot.
I have made a for loop, which I ant to loop thorugh each doubl ein the cell array and plot against time. This does make 15 plots but I think it is plotting the same data 15 times. Would anybody be able to help?
%% load all the excel files in, using the same method as csv
files = dir('*.xlsx'); %dir lists the files in a folder. In the specified folder, recognise all the .xlsx files
num_files = length(files); %specify how many files have been found and call it num_files
grainsleft = cell(length(files), 1); %create a cell array the same length as the number of files in one column
%for all the files found in the specified folder, read the tables of data and fill the empty cell array 'results' with the data in each .xlsx file
for a = 1:num_files
grainsleft{a} = readtable(files(a).name);
end
%% time steps
%number of xlsx files = number of time steps the model is run for (seconds)
totalruntime = 3001
time = 1:totalruntime;
%% mass efflux vs. time
%subplot all the mass efflux plots
for b = 1:length(grainsleft)
g{b} = grainsleft{b}(:,1);
g_array{b} = table2array(g{b});
gl{b}= g_array{b}
end
for c = 1:length(gl)
subplot(4,4,c)
bar(time,gl{c}(:,1), 0.01, 'EdgeColor', 'r')
hold on
end
  2 Comments
C.G.
C.G. on 12 Jan 2021
Thank you for your response.
I have worked out how to do some of what I required.
I have managed to plot my 15 different graphs now. But in 'grainsleft' I have included the experiment number as the data heading at the top of each cell, e.g.'COR0.1 SC10 FC0.1'. I would like to use this experiment number to title the respective graphs. I have attached my file with the grains left saved to it.
Would you be able to help?

Sign in to comment.

Accepted Answer

Adam Danz
Adam Danz on 12 Jan 2021
Edited: Adam Danz on 12 Jan 2021
This is the result I get and the plots are clearly different (the shape and the y-axis-limit).
For comparison, equate the axis limits using linkaxes()
ax = gobjects(size(gl)); % <-- added
for c = 1:length(gl)
ax(c) = subplot(4,4,c); % <-- save handles
bar(time,gl{c}(:,1), 0.01, 'EdgeColor', 'r')
hold on
grid on % <-- added
end
linkaxes(ax) % <-- added
By the way, the script you shared could be cleaned up and reduced to,
% New version that replaces your entire script
files = dir('*.xlsx');
num_files = numel(files);
grainsleft = cell(numel(files), 1);
for a = 1:num_files
grainsleft{a} = readtable(files(a).name);
end
totalruntime = 3001;
time = 1:totalruntime;
figure();
ax = gobjects(size(grainsleft));
for i = 1:numel(grainsleft)
ax(i) = subplot(4,4,i);
bar(ax(i), time, grainsleft{i}{:,1}, 0.01, 'EdgeColor', 'r')
grid(ax(i),'on')
end
linkaxes(ax)
Lastly, consider using a histogram, or fill instead of bar. The bar plot is not representitive of your actual data. It hides important features.
fill(ax(i),[time(:);flipud(time(:))], [grainsleft{i}{:,1}(:);zeros(numel(time),1)],'r','EdgeColor','none')
% or
histogram(ax(i),'BinEdges',timeBins, 'BinCounts',grainsleft{i}{:,1},'EdgeColor','none','FaceColor','r')
Results of fill
  4 Comments
Adam Danz
Adam Danz on 12 Jan 2021
It looks like this is what you're trying to do
signal = grainsleft{d}{:,1};
  • grainsleft is a cell array.
  • grainsleft{d} is a table
  • grainsleft{d}{:,1} is the first column of the table

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!