Sometimes we need to read and process Excel files in a folder or multiple folders. So how to achieve it?
Take reading Excel files in a folder as an example.
File address: E:\Study of Lab\Coding\Experience DatePhoto\Excel
The method of batch reading excel under the file address is:
excel_path= 'E:\Study of Lab\Coding\Experience DatePhoto\Excel\'; %文件夹路径
img_path_list = dir(strcat(excel_path,'*.xlsx'));
%%dir('.')列出当前目录下所有子文件夹和文件%
img_num = length(img_path_list);
%文件的长度
Newexcel_path='E:\Study of Lab\Coding\Experience DatePhoto\diff2method\';
%若想对读取的Excel进行处理,新的excel存放地址
img_name =sort_nat({img_path_list.name}); %排序后的文件名
for i=1:img_num
P_data(:,:,i) = xlsread([excel_path,num2str(i),'.xlsx']);
diffdate = diff(P_data,2) %以对数据进行2阶偏微分处理
end
The screenshot of the processing result part is: The
xlswrite() function is needed to save the data as an Excel file (.xlsx).
例如:
xlswrite(strcat(file_path,int2str(data1),’.xlsx’), xyz,‘A,K’);
Among them, strcat is Strings Catenate, which connects strings horizontally.
Role: connect multiple characters into a single string
strcat(file_path,int2str(data1): Store the Excel file with data1 as the naming rule in the file_path folder, such as 0.xlsx,1.xlsx,...,100.xlsx.
The content of the stored data is what xyz contains
The storage range of the Excel file is from A to K (column 1 to column 11)
If you want to process the read excel, you can also write the processing module in the above for loop.
for ...
...
%处理模块
...
end
----------------------------Ending----------------------------