closing specific excel file
11 views (last 30 days)
Show older comments
Hi all, I am opening some CSV files and then choose some columns and take an average of the last data. At the same time, I separate the name of the CSV files.
Now first, I write the name of the files in the first row. Then I want to write the numbers in matrix M in the second row. But I got an error that the file is open
clc
clear
files = subdir('C:\Users\roozm\Desktop\New folder\*.csv');
for i=1:numel(files)
filename = files(i).name;
sheet = 1;
[filepath,name,ext] = fileparts(filename);
N{i,1}= name;
subset = xlsread(filename,sheet,'A:C');
subset2 = xlsread(filename,sheet,'F:G');
subset3 = xlsread(filename,sheet,'K:N');
subset_merged=[subset,subset2,subset3];
subset_tot=subset_merged([end-6:end-2],:);
M=mean(subset_tot);
end
N=N';
xlswrite('resultset',N);
Can you please advise how can I close the excel file that I have the name in it, and then write the numbers in matrix M in it?
5 Comments
Walter Roberson
on 19 Nov 2024 at 18:20
files = subdir('C:\Users\roozm\Desktop\New folder\*.csv');
subdir() is not a Mathworks function.
filename = files(i).name;
subset = xlsread(filename,sheet,'A:C');
You appear to be fetching directory information about files in C:\Users\roozm\Desktop\New folder but you are attempting to read the files out of the current directory. Unless, that is, subdir is incompatible with dir and returns the entire filename in the name field.
Answers (1)
hela
on 19 Nov 2024 at 12:36
Edited: Walter Roberson
on 19 Nov 2024 at 18:13
clear all
close all
clc
filname=('C:\Users\dell\Desktop\Data_Battery\Battery_RUL.csv');% % Spécifier le chemin vers le fichier CSV
data= readtable(filname);% Importer les données dans une table
disp(data);% Afficher les premières lignes du tableau
% Suppress warnings (equivalent to warnings.filterwarnings('ignore') in Python)
warning('off', 'all');
% Loop through the items
for i = 1:length(files)
% Skip '.' and '..' (the current and parent directory)
if strcmp(files(i).name, '.') || strcmp(files(i).name, '..')
continue;
end
% Get full path of the current item
fullPath = fullfile(dirPath, files(i).name);
% If it's a directory, recursively call listFiles
if files(i).isdir
% Recursively search the subdirectory
listFiles(fullPath);
else
% If it's a file, display its full path
disp(fullPath);
end
end
% MATLAB plotting (no need to import anything like matplotlib or seaborn)
1 Comment
Walter Roberson
on 19 Nov 2024 at 18:16
for i = 1:length(files)
No variable files has been defined.
fullPath = fullfile(dirPath, files(i).name);
No variable dirPath has been defined.
listFiles(fullPath);
No function listFiles has been defined.
See Also
Categories
Find more on Spreadsheets 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!