How to perform processinng on .csv files used by my code for each person in one go without defining functions for each person in Matlab?

时间:2018-07-24 10:18:08

标签: matlab

I have a main code named as process.m in which I define the path to 4 different .csv files for calculating values for each person. If I have a list of 30 persons and I don't want to define process.m as a function for each person, how can I do the processing for all the persons in one go. I want some idea by which process.m itself picks files for one person, then generate the results, then move to other person, pick his .csv files, generate the result and so on. A breif outline of my code is attached here that would project the problem.

file1='Joseph_front.csv';
file2='Joseph_side.csv';
file3='Joseph_knee.csv';
file4='Joseph_back.csv';


A1=initiate2(file1); %initiate2 function reads the csv and perfoms some filtering operations on image in .csv format
A2=initiate2(file2);
A3=initiate2(file3);
A4=initiate2(file4);

%%omitted large part of code
cal(1) = p+q+r*s;
cal(2) = p+q+r+s;
cal(3) = p+q+r-s;
cal=cal'

%code to write the calculation in excel file
excelfile= 'test.xlsx';
xlswrite(excelfile,ValuesInInches,'Joseph_data',posColumn);

Describing more i want my code to process for 30 people all at once by selecting and picking the files itself, although i have done this operation by making the same code as a function for each person, but that is not very efficient as when I have to make a small change I have to make it in every function that means one change needs to be edited in 30 functions. Please suggest an efficient way to do it. Note: All persons .csv files are named in the same manner and exist in the current folder.

1 个答案:

答案 0 :(得分:0)

我假设所有文件都放在一个目录中,没有其他文件。

这部分代码将获取可用的文件名。

listFiles = dir('path of the directory');
filenames = strings;                     % an empty string array to save the filenames
j = 1;
for i = 1:1:length(listFiles)            
    if ~listFiles(i).isdir               % to avoid the directory names
        filenames(j,1) = listFiles(i).name;
        j = j+1;
    end
end

现在,每个人都有4个文件。因此,循环一次应包含4个文件。

for ii = 1:4:length(filenames)
    file1=filename(i);
    file2=filename(i+1);
    file3=filename(i+2);
    file4=filename(i+3);

    %% continue with your code

end