Trouble getting multiple text file loop to work
Afficher commentaires plus anciens
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
Réponse acceptée
Plus de réponses (1)
Y.Nadiree
le 11 Jan 2013
0 votes
Catégories
En savoir plus sur MATLAB dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!