How to modify the code below so it can loop over certain files rather then 1?

Hi, Below is the code which is reading one csv file I want to modify this code so it will read 100 csv files and produces 100 values of following [xc,yc,Re,a]. Quick modification would be helpful as deadline in an hour.
Code:
XY = dlmread('contours0.137.csv');
%
[xc,yc,Re,a] = circfit(XY(:,1),XY(:,2))
% reconstruct circle from data
n=100;
th = (0:n-1)/n*2*pi;
xe = Re*cos(th)+xc; ye = Re*sin(th)+yc;
figure(1),
plot(XY(:,1),XY(:,2),'b*',xe,ye,'r-'),
title(' measured fitted circles')
legend('measured','fitted')
text(xc-Re*0.9,yc,sprintf('center (%g , %g ); R=%g',xc,yc,Re))
% xlabel x, ylabel y
axis equal

2 件のコメント

jessupj
jessupj 2020 年 10 月 27 日
編集済み: jessupj 2020 年 10 月 27 日
try a for loop
muhammad choudhry
muhammad choudhry 2020 年 10 月 27 日
I have applied the for loop here but it is only reading first file and producing only results from the first file. Below is the code:
csvFile = dir("*.csv") ;
N = length(48) ;
for i = 1:N
data = readtable(csvFile(i).name) ;
XY = data
[xc,yc,Re,a] = circfit(XY(:,1),XY(:,2))
% reconstruct circle from data
n=100;
th = (0:n-1)/n*2*pi;
xe = Re*cos(th)+xc; ye = Re*sin(th)+yc;
end

サインインしてコメントする。

 採用された回答

KSSV
KSSV 2020 年 10 月 27 日
編集済み: KSSV 2020 年 10 月 29 日
csvFiles = dir("*.csv") ;
N = length(csvFiles) ;
C = zeros(N,3) ;
for i = 1:N
XY = dlmread(csvFiles(i).name);
[xc,yc,Re,a] = circfit(XY(:,1),XY(:,2));
C(i,:) = [xc yc Re] ;
% reconstruct circle from data
n=100;
th = (0:n-1)/n*2*pi;
xe = Re*cos(th)+xc; ye = Re*sin(th)+yc;
figure(1),
hold on
plot(XY(:,1),XY(:,2),'b*',xe,ye,'r-')
end
title(' measured fitted circles')
legend('measured','fitted')
text(xc-Re*0.9,yc,sprintf('center (%g , %g ); R=%g',xc,yc,Re))
% xlabel x, ylabel y
axis equal

5 件のコメント

jessupj
jessupj 2020 年 10 月 27 日
In the workds of one of your fellow MVP's:
muhammad choudhry
muhammad choudhry 2020 年 10 月 27 日
Hi,
Please see the attached image. With my code I pasted and with your modified code, I am still only getting one value Re. If you look at workspace you will understand what is it doing. So lets say if I am asking the folder to read all the files in the folder which is 48 hence it should read the data from 48 files and produces 48 values of Re but I am not sure why this is not happening here.
Thanks in advance for the help!
KSSV
KSSV 2020 年 10 月 27 日
You will get only one value and that would be the last. Note that you are not saving those values for every loop. You have to intialize a matrix and save all the values.
Example:
A = zeros(5,1) ;
for i = 1:5
A(i) = rand ;
end
muhammad choudhry
muhammad choudhry 2020 年 10 月 28 日
What am I doing wrong here? I have no idea! to me it should work and give me values of [xc,yc,re,a] from each file I am asking it to read.
Code:
csvFiles = dir("*.csv") ;
N = length(csvFiles) ;
for i = 1:N
XY = readtable(csvFiles(i).name);
[xc,yc,Re,a] = zeros(48,1)
for i = 1:48
[xc,yc,Re,a] = circfit(XY(:,1),XY(:,2))
[xc,yc,Re,a](i) = rand;
end
% reconstruct circle from data
n=100;
th = (0:n-1)/n*2*pi;
xe = Re.*cos(th)+xc; ye = Re.*sin(th)+yc;
end
Error:
Error: File: test_circlefit.m Line: 12 Column: 14
Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for
mismatched delimiters.
KSSV
KSSV 2020 年 10 月 29 日
I have edited the answer.....center and radius of circle is stored in C.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File Exchange で Graphics Performance についてさらに検索

質問済み:

2020 年 10 月 27 日

コメント済み:

2020 年 10 月 29 日

Community Treasure Hunt

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

Start Hunting!

Translated by