plot graph from cell array

4 visualizaciones (últimos 30 días)
C.G.
C.G. el 11 de En. de 2021
Editada: Adam Danz el 13 de En. de 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 comentarios
Adam Danz
Adam Danz el 11 de En. de 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.
C.G.
C.G. el 12 de En. de 2021
Editada: C.G. el 12 de En. de 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?

Iniciar sesión para comentar.

Respuesta aceptada

Adam Danz
Adam Danz el 12 de En. de 2021
Editada: Adam Danz el 12 de En. de 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 comentarios
C.G.
C.G. el 12 de En. de 2021
Editada: C.G. el 12 de En. de 2021
Fantastic thank you.
I have been working my way through the cleaned up and reduced code you provided to ensure I understand it. However, I have a second set of graphs that are plotted after the fill graphs, which I cannot get to work now the variables have changed. Would you be able to help me with this?
my original code was:
%fft/power spectra
%set up basic parameters
figure(2)
%Loop through all the files in g1 and plot the respective power spectra
for d = 1:18
signal = gl(:,d);
N = length(signal); %number of samples in the signal
fs = 100; %how many samples are taken per second
fnyquist = fs/2; %nyquist frequency
%plot a single sided power spectrum with log frequency axis
%abs is the magnitude values
%fft is a built in function that determines magnitude and phases of sinusoids present
%put number <size (grains) after signal, can change resoltuion
%no number, matlab will do the whole length of the vector
X_mags = abs(fft(signal));
%assign frequency axis values, starting with bins from 0:N-1
bin_vals = [0 : N-1];
%to convert from bin to Hz use the standard formula: bin*fs/N, where fs and
%N are defined at the beginning
fax_Hz = bin_vals*fs/N;
%ceil rounds N/2 to the nearest integer greater or equal to that element; e.g. -1.9 is rounded to -2.0
N_2 = ceil(N/2);
%create a plot where the x axis has a log scale (semilogx)
%x axis is log Hz, ranging from 1:N2
subplot(5,5,d)
%y axis converts the values of X_mags to dB using 20*log10
semilogx(fax_Hz(1:N_2), 20*log10(X_mags(1:N_2)))
xlabel('Frequency (Hz)')
ylabel('Power (dB)');
axis tight
end
Adam Danz
Adam Danz el 12 de En. de 2021
Editada: Adam Danz el 13 de En. de 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

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Discrete Data Plots en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by