plot graph from cell array
4 views (last 30 days)
Show older comments
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
Adam Danz
on 11 Jan 2021
At quick glance it looks OK but I don't put much weigh on human-read code. If you save grainsleft to a mat file and attach it, we can run the code.
Accepted Answer
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

More Answers (0)
See Also
Categories
Find more on Discrete Data Plots 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!