필터 지우기
필터 지우기

read csv file and output csv file

조회 수: 2 (최근 30일)
arian hoseini
arian hoseini 2023년 12월 5일
댓글: arian hoseini 2023년 12월 5일
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

답변 (1개)

Voss
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
  댓글 수: 1
arian hoseini
arian hoseini 2023년 12월 5일
thanks i think it fixed the output problem but i have another problem....E...how can i put E and sigma in something like P...cause after i fixed k i got index in position 2...for line 12 (I = data(:,3)/(56);)

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

카테고리

Help CenterFile Exchange에서 Data Import and Export에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by