How to create a cell array with constant data input without overwriting?

I’m running a code for different files, however I need to sore the results in a cell array without overwriting the info in each cell, I mean every time I run the code for a different file as a result it creates a second row of data in the cell array. Thanks.

6 个评论

Couldn't you create a new dimension in your cell array?
For example:
x(end+1,:,:) = y;
@Nicolas Broch here is the code , everytime I run this for a different file, it overwrites the variables mn1,md1, and so on,, the idea is to accumulate the results in a cell array without overwriting the previous one.
tab8=uitab('Title','Position of layers (Distribution) (km)');
ax8=axes(tab8);
sgtitle(tab8, 'Position of layers (Distribution) (km)');
mnn1=cellfun(@mean,locs);
ksdensity(mnn1,'Parent',ax8);
ax8.Children(1).Color = 'Black';
ax8.Children(1).LineWidth = 1.2;
xlabel(['Position of layers in altitude/km LT 24h, date: ',R(1).Info.Name(1:8)],'FontSize',9,'FontName','Arial','color','default');
yticks('');
ax8.XGrid = 'on';
ax8.GridAlpha = 0.9;
ax8.GridLineStyle = '-';
ax8.GridColor = [0.3010 0.7450 0.9330];
ax8.Color = 'y';
ylim(ax8,[-inf inf]);
hold;
% mean
mn1=mean(mnn1,'omitnan');
mn11=xline(mn1,'Parent',ax8);
ax8.Children(1).Color = 'Red';
ax8.Children(1).LineWidth = 1.2;
ax8.Children(1).LineStyle = '-';
hold;
% median
md1=median(mnn1,'omitnan');
md11=xline(md1,'Parent',ax8);
ax8.Children(1).Color = '#7E2F8E';
ax8.Children(1).LineWidth = 1.2;
ax8.Children(1).LineStyle = '--';
hold;
%mode
mod1=mode(mnn1,'all');
mod11=xline(mod1,'Parent',ax8);
ax8.Children(1).Color = 'cyan';
ax8.Children(1).LineWidth = 1.2;
ax8.Children(1).LineStyle = ':';
hold;
legend('Layering distribution',sprintf('Mean(km) :%7.2f',mn1),sprintf('Median(km) :%7.2f',md1),sprintf('Mode(km) :%7.2f',mod1),'FontSize',7,'Location','northeast','NumColumns',1);
legend('boxoff');
hold;
%% width of layers per day
tab9=uitab('Title','Width of layers (km)');
ax9=axes(tab9);
sgtitle(tab9, 'Width of layers (km)');
mnn11=cellfun(@mean,widths);
histfit(mnn11);
%mean
mn11=mean(mnn11,'omitnan');
mn111=xline(mn11,'Parent',ax9);
ax9.Children(1).Color = 'Green';
ax9.Children(1).LineWidth = 1.2;
ax9.Children(1).LineStyle = '-';
hold;
%median
md11=median(mnn11,'omitnan');
md111=xline(md11,'Parent',ax9);
ax9.Children(1).Color = '#7E2F8E';
ax9.Children(1).LineWidth = 1.2;
ax9.Children(1).LineStyle = '--';
hold;
%mode
mod11=mode(mnn11,'all');
mod111=xline(mod11,'Parent',ax9);
ax9.Children(1).Color = 'cyan';
ax9.Children(1).LineWidth = 1.2;
ax9.Children(1).LineStyle = ':';
hold;
xlabel(['Width of layers (km), LT 24h date: ',R(1).Info.Name(1:8)],'FontSize',9,'FontName','Arial','color','default');
yticks('');
legend('Data','Normal distribution',sprintf('Mean(km) :%7.2f',mn11),sprintf('Median(km) :%7.2f',md11),sprintf('Mode(km) :%7.2f',mod11),'FontSize',7,'Location','best');
legend('boxoff');
ax9.Color = 'Yellow';
hold;
Sir @Star Strider , anything probably you want to comment? Thanks.
@Stephen Cobeldick Apparently, it doesn't explain the fact how you can create a cell array or a vector without overwriting the results of the previous run.
"Apparently, it doesn't explain the fact how you can create a cell array or a vector without overwriting the results of the previous run. "
Here, I highlighted it for you:
How to use indexing in loops is explained here:
See also:

请先登录,再进行评论。

回答(1 个)

num_files = 10; cell_data = {}; for k = 1:num_files cell_data{end+1} = filenames(k);%example %file names end

类别

帮助中心File Exchange 中查找有关 Matrix Indexing 的更多信息

产品

版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by