在循环中向单元格数组添加不同长度的列并标记它们

时间:2018-05-04 02:53:18

标签: matlab structure cell-array

我有一个for循环,我从.csv文件中提取一些数据:

    % Startup
clearvars;

stress_folder = uigetdir(path,'Select STRESS folder');
strain_folder = uigetdir(path,'Select STRAIN folder');

prompt = {'Plot name:','x label (strain):','y label (stress):','Name of STRAIN values:','Name of STRESS values:'};
dlg_title = 'Input';
num_lines = 1;
defaultans = {'Title','x label','y label','Surface component 1.avg(epsY) [True strain]','Stress'};
answer = inputdlg(prompt,dlg_title,num_lines,defaultans);

filePattern = sprintf('%s/*.csv', stress_folder);
stress_file_list = dir(filePattern);
filePattern = sprintf('%s/*.csv', strain_folder);
strain_file_list = dir(filePattern);

name_plot = answer{1};
x_label = answer{2};
y_label = answer{3};
strain_col = answer{4};
stress_col = answer{5};

des_cols = {stress_col,strain_col};
smallest_n = 100000;
avg_stress_values = [];
avg_strain_values = [];
ii=0;

% Determine smallest set of data
for file = stress_file_list'

    M = xlsread(fullfile(stress_folder,file.name));
    [row,col] = size(M);
    if smallest_n > row
        smallest_n = row;
    end
end
for file = strain_file_list'
    M=xlsread(fullfile(strain_folder,file.name));
    [row,col]=size(M);
    if smallest_n > row
        smallest_n = row;
    end
end

% Extract columns
for file = stress_file_list'
    ii = ii + 1;
    [n,s,r] = xlsread(fullfile(stress_folder,file.name));
    [row, col] = find(strcmpi(s,stress_col));
    stress_values = n(1:end,col);
    avg_stress_values(:,ii)=stress_values(1:smallest_n);
    % place for code to store stress_values in a cell array or structure array at column index ii
end
ii=0;
for file = strain_file_list'
    ii = ii + 1;
    [n,s,r] = xlsread(fullfile(strain_folder,file.name));
    [row, col] = find(strcmpi(s,strain_col));
    strain_values = n(1:end,col);
    avg_strain_values(:,ii)=strain_values(1:smallest_n);
    % place for code to store strain_values in a cell array or structure array at column index ii
end

% Plot the averages
ylabel({y_label});
xlabel({x_label});
title({name_plot});
colormap(gray);
hold off;

avg_strain_values = mean(avg_strain_values,2);
avg_stress_values = mean(avg_stress_values,2);
plot(avg_strain_values,avg_stress_values);
set(gca,'FontSize',20);
ylabel({y_label});
xlabel({x_label});
title({name_plot});

在提取列部分中,我想将所有数据存储在...中,因为缺少更好的单词,然后绘制它。根据我的理解,我可以使用结构数组或单元格数组,但我不确定哪一个更适合我的目的。我不确定是否更好地创建两个单元阵列,一个用于应力应变一个,或者一个结构数组,其中包含应力和应变场以及每组数据的子场。

问题是从.csv文件中提取的数据列的长度不是相同的长度,因此很难在每次迭代时追加列。我可以在开头创建一个循环,找到最大的列大小,然后创建该大小的结构/单元格数组(如果有帮助的话)。

0 个答案:

没有答案