Code to handle excel using Matlab
Show older comments
I want to handle excel using Matlab because there are lots of excel files and handling these manually is quite tedious.
Uploaded link is showing process that I want to repeat by using Matlab but I'm not good at coding and I don't know function for excel in Matlab.
This video is treating excel file whose name is 'Data001' and I want to repeat this process upto 'Data040'
How can I do?
Thank you~!
3 Comments
Voss
on 6 Jan 2023
What is the extension of the files? Do they have no extension? Or is the extension something like ".txt" and the extension is hidden in Windows File Explorer?
상호 고
on 6 Jan 2023
Voss
on 6 Jan 2023
I understand it's a text file, but it's important to know the actual extension.
Answers (1)
Voss
on 6 Jan 2023
Try this:
% use the directory where your files are here:
input_file_path = 'C:\2021_11_15\Inlet';
% use the directory where you want the new files to go here:
output_file_path = 'C:\2021_11_15\Inlet_after_handling';
% get info about the relevant files in the input directory
files = dir(fullfile(input_file_path,'Data*.txt')); % if they have .txt extension
% remove any directories returned by dir
% (might happen if the files have no extension):
files([files.isdir]) = [];
% construct full-path file names of input and output files:
input_file_names = fullfile(input_file_path,{files.name});
output_file_names = fullfile(output_file_path,{files.name});
% read each input file and write the corresponding output file:
for ii = 1:numel(files)
A = readmatrix(input_file_names{ii},'FileType','text'); % 'FileType','text' is necessary if the files have no extension
% writing only columns 1,3,2,6,5 in that order
writematrix(A(:,[1 3 2 6 5]),output_file_names{ii},'Delimiter','\t','FileType','text');
end
Categories
Find more on Data Import from MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!