closing specific excel file

13 Ansichten (letzte 30 Tage)
Roozbeh Yousefnejad
Roozbeh Yousefnejad am 30 Mai 2018
Kommentiert: Walter Roberson am 19 Nov. 2024
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 Kommentare
Roozbeh Yousefnejad
Roozbeh Yousefnejad am 30 Mai 2018
great, thanks
Walter Roberson
Walter Roberson am 19 Nov. 2024
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.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

hela
hela am 19 Nov. 2024
Bearbeitet: Walter Roberson am 19 Nov. 2024
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 Kommentar
Walter Roberson
Walter Roberson am 19 Nov. 2024
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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu File Operations finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2018a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by