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

Walter Roberson
Walter Roberson am 30 Mai 2018
Which file is it saying is already open? Which directory should the file go into? Should it go into your current directory, or should it go into C:\Users\roozm\Desktop\New folder ?
Is it the matrix N that you want to write?
Roozbeh Yousefnejad
Roozbeh Yousefnejad am 30 Mai 2018
I think I found the mistake. I have opened the file myself and it seems that xlswrite won't open files and it writes into an excel file without opening it Now my issue is that the names are written in the first row of this excel file, and I want to start from the second row and write the numbers. How can I do that?
dpb
dpb am 30 Mai 2018
Use the optional Range inputs to xlswrite
Roozbeh Yousefnejad
Roozbeh Yousefnejad am 30 Mai 2018
great, thanks
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

0 Stimmen

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

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

Produkte

Version

R2018a

Tags

Gefragt:

am 30 Mai 2018

Kommentiert:

am 19 Nov. 2024

Community Treasure Hunt

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

Start Hunting!

Translated by