closing specific excel file

조회 수: 11 (최근 30일)
Roozbeh Yousefnejad
Roozbeh Yousefnejad 2018년 5월 30일
댓글: Walter Roberson 2024년 11월 19일 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
Roozbeh Yousefnejad
Roozbeh Yousefnejad 2018년 5월 30일
great, thanks
Walter Roberson
Walter Roberson 2024년 11월 19일 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.

댓글을 달려면 로그인하십시오.

답변 (1개)

hela
hela 2024년 11월 19일 12:36
편집: Walter Roberson 2024년 11월 19일 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
Walter Roberson
Walter Roberson 2024년 11월 19일 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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

태그

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by