read csv file and output csv file

4 Ansichten (letzte 30 Tage)
arian hoseini
arian hoseini am 5 Dez. 2023
Kommentiert: arian hoseini am 5 Dez. 2023
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

Antworten (1)

Voss
Voss am 5 Dez. 2023
Bearbeitet: Voss am 5 Dez. 2023
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 Kommentar
arian hoseini
arian hoseini am 5 Dez. 2023
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);)

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Downloads finden Sie in Help Center und File Exchange

Tags

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by