closing specific excel file

11 visualizaciones (últimos 30 días)
Roozbeh Yousefnejad
Roozbeh Yousefnejad el 30 de Mayo de 2018
Comentada: Walter Roberson el 19 de Nov. de 2024 a las 18:20
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 comentarios
Roozbeh Yousefnejad
Roozbeh Yousefnejad el 30 de Mayo de 2018
great, thanks
Walter Roberson
Walter Roberson el 19 de Nov. de 2024 a las 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.

Iniciar sesión para comentar.

Respuestas (1)

hela
hela el 19 de Nov. de 2024 a las 12:36
Editada: Walter Roberson el 19 de Nov. de 2024 a las 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 comentario
Walter Roberson
Walter Roberson el 19 de Nov. de 2024 a las 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.

Iniciar sesión para comentar.

Etiquetas

Productos


Versión

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by