read csv file and output csv file
2 次查看(过去 30 天)
显示 更早的评论
here i have 3 csv file so i should get 3 csv output file but i only get 1 and E data shouldnt be same but it is...
clc
clear all
datapath = 'I:\A';
files = dir(fullfile(datapath,'*.csv'));
for k = 1:numel(files)
filename = fullfile(files(k).folder,files(k).name);
data = readmatrix(filename,"NumHeaderLines",2);
V = data(:,2);
I = data(:,3)/(56);
VVV=V(104:131)*1000/6;
E=VVV/30;
III=I(104:131);
[m,n]=size(III);
k=1;
for i=1:n
sig(i)=(III(i)*1000/(4*pi))/(VVV(i)/0.3);
k=k+1;
end
for i=1:n
P=zeros(m,2);
P(:,1)=E;
P(:,2)=sig(i);
plot(E,sig);
hold on;
end
outfile = fullfile(datapath,['out',num2str(k,'%03d'),'.csv']);
writematrix(P,outfile)
end
0 个评论
回答(1 个)
Voss
2023-12-5
编辑:Voss
2023-12-5
You are overwriting k inside the k for-loop:
for k = 1:numel(files)
filename = fullfile(files(k).folder,files(k).name);
data = readmatrix(filename,"NumHeaderLines",2);
% ...
k=1;
for i=1:n
sig(i)=(III(i)*1000/(4*pi))/(VVV(i)/0.3);
k=k+1;
end
% ...
% now k has the value just calculated from the while loop, not the
% value given by which iteration of the for loop this is.
outfile = fullfile(datapath,['out',num2str(k,'%03d'),'.csv']);
writematrix(P,outfile)
end
Instead, use a different variable name for those two different meanings of k, e.g.:
for ii = 1:numel(files)
filename = fullfile(files(ii).folder,files(ii).name);
data = readmatrix(filename,"NumHeaderLines",2);
% ...
k=1;
for i=1:n
sig(i)=(III(i)*1000/(4*pi))/(VVV(i)/0.3);
k=k+1;
end
% ...
outfile = fullfile(datapath,['out',num2str(ii,'%03d'),'.csv']);
writematrix(P,outfile)
end
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Import and Export 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!