read csv file and output csv file

3 views (last 30 days)
arian hoseini
arian hoseini on 5 Dec 2023
Commented: arian hoseini on 5 Dec 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

Answers (1)

Voss
Voss on 5 Dec 2023
Edited: Voss on 5 Dec 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 Comment
arian hoseini
arian hoseini on 5 Dec 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);)

Sign in to comment.

Categories

Find more on Downloads in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!