Trouble getting multiple text file loop to work

Hi, I have spent about two weeks getting upto this point and cant seem to crack it. I get an error message of ''Undefined function or variable 'II'.' and variables I l S also as undefined so the problem seems to be with I l s and not II as such.
I have about 20 or so text files that I need to perform simple interpolation and integration. the text files have one header, and three columns of numbers which are tab delimited. I am pretty sure the problem is textscan or my use of it because previously I had:
[I,l,s]=textread('filename','%f %f %f','headerlines',1);
and this gave me the column outputs nicely but I could not get a loop to work with textread to go through all files in a directory.
if true
%
path='Z:\Year 3\FEA assignment\Matlab';
D=dir(fullfile('path', '*.txt'));
for k=1:numel(D)
fid=fopen(fullfile(path, D(k).name), 'r');
data=textscan(fid,'%f %f %f','headerlines',1)
data{1}=I;
data{2}=l;
data{3}=s;
fclose(fid);
for i=1:l(end)
if s(i)<0
na=l(i);
i;
% linear interpolate distance to zero stress
p1=[l(i-1) s(i-1)];
p2=[l(i) s(i)];
% Equation of line - y=gradx+c
grad=(p2(2)-p1(2))/(p2(1)-p1(1));
c=p1(2)-grad*(p1(1));
% Set y=0 to get neutral axis position
x=-c/grad ;
fprintf('Distnce to Neutral axis is %.2f mm \n', x)
break
end
% Remove all -ve stress from vectors
for j=1:i
II(j)=I(j);
ll(j)=l(j);
ss(j)=s(j);
end
end
end
% Combine All positive with interpolated point
II=[II II(end)+1];
ll=[ll x];
ss=[ss 0];
% Convert to standard units
L=ll/(1e3); % In m
S=ss*(1e6); % In Pa
A=trapz(L,S); % Integrate Points V by spacing of L
% Find average stress (height)
Snom=(A)/L(end); % Use only distance to neutral axis
SCF=S/Snom;
m=max(SCF);
fprintf('Nominal Stress= %.3g(Pa)\nKt= %.2f \n',Snom, m)code

 Respuesta aceptada

Y.Nadiree
Y.Nadiree el 11 de En. de 2013
Ok, managed to loop through all files by changing the start bit to
all_files=dir('*.txt');
for k=1:1:length(all_files);
filename=all_files(k).name;
fid=fopen(filename,'r');
C=textscan(fid,'%f %f %f','Headerlines',1);
fclose(fid);
I=C{1}; % Index
l=C{2}; % Length(mm)
s=C{3}; % Stress(MPa)
end
rest of code same as before
However one little problem remains which is for some reason it reuses some variables from previous loops because if I do the operation on a single file the results is different than when that same file is done after others in a loop. I tried putting clear before the final end command but seems to break the loop giving error ??? Undefined variable "all_files" or class "all_files" after the first file.
I know it is reusing data from previous files also because the plot of the graphs look odd and the same as when I manually ran some files without clearing variables so that is my problem for now.

Más respuestas (1)

Y.Nadiree
Y.Nadiree el 11 de En. de 2013

0 votos

Solved the problem, before the final end just added
clearvars -except all_files
Hope it helps someone else.

Categorías

Más información sobre MATLAB en Centro de ayuda y File Exchange.

Preguntada:

el 10 de En. de 2013

Community Treasure Hunt

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

Start Hunting!

Translated by