read csv file and output csv file

3 visualizaciones (últimos 30 días)
arian hoseini
arian hoseini el 5 de Dic. de 2023
Comentada: arian hoseini el 5 de Dic. de 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

Respuestas (1)

Voss
Voss el 5 de Dic. de 2023
Editada: Voss el 5 de Dic. de 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 comentario
arian hoseini
arian hoseini el 5 de Dic. de 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);)

Iniciar sesión para comentar.

Categorías

Más información sobre Data Import and Export en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by