How to skip or delete lines in text if statement

12 Ansichten (letzte 30 Tage)
Kristin Bogar
Kristin Bogar am 18 Nov. 2021
Bearbeitet: Image Analyst am 18 Nov. 2021
[num,mast] = xlsread('CR_Creo-Materials-MASTER - Copy for code.xlsx','Sheet1' );
baseName = [mast([14:17],4)];
name =convertCharsToStrings(baseName);
Name= name;
stringm= convertCharsToStrings(mast);
%values for input
D=num([3:end],11); %density
PR=num([3:end],13);%posidens ratio needs to be e-0x
Y=num([3:end],15); %youngs
TY=num([3:end],17); %tensile yeaild e+0x
TU=num([3:end],19); %tensile ULT e+0x
TC=num([3:end],21);%thermal concuctivity e+0x
TEX=num([3:end],23); %thermal expansion e-0x
%conversions
Y= Y*1000000; %youngs msi to psi
TY=TY*1000; %tensile yeaild e+0x KSI to PSI
TU=TU*1000; %tensile ULT e+0x ksi to psi
%sources
DS= mast([14:end],13);%density source
PS= mast([14:end],15);%Poissions source
YS= mast([14:end],17);%Youngs source
TYS= mast([14:end],19);%tensile yeild source
TUS= mast([14:end],21);%tensile ultimate source
TS= mast([14:end],23);%thermal source
CS= mast([14:end],25);%cefficient source
DS= convertCharsToStrings(DS);%density source
PS= convertCharsToStrings(PS);%Poissions source
YS= convertCharsToStrings(YS);%Youngs source
TYS= convertCharsToStrings(TYS);%tensile yeild source
TUS= convertCharsToStrings(TUS);%tensile ultimate source
TS= convertCharsToStrings(TS);%thermal source
CS= convertCharsToStrings(CS);%cefficient source
%info
MT= mast([14:end],8); %Material Type
FO= mast([14:end],9); %Form
DE= mast([14:end],10); %description
AD= mast([14:end],26); %additional info
SD= mast([14:end],28); %Source date
ST= mast([14:end],29); %source temp
DI= mast([14:end],31); %Disclaimer General
MA= mast([14:end],33); %Manufacture
GU= mast([14:end],34); %Genral use
SF= mast([14:end],36); %Special Features
AC= mast([14:end],38); %acronyms
DIS= mast([14:end],40); %Disclaimer source
MT= convertCharsToStrings(MT); %Material Type
FO= convertCharsToStrings(FO); %Form
DE= convertCharsToStrings(DE); %description
AD= convertCharsToStrings(AD); %additional info
SD= convertCharsToStrings(SD); %Source date
ST= convertCharsToStrings(ST); %source temp
DI=convertCharsToStrings(DI); %Disclaimer General
MA= convertCharsToStrings(MA); %Manufacture
GU= convertCharsToStrings(GU); %Genral use
SF= convertCharsToStrings(SF); %Special Features
AC= convertCharsToStrings(AC); %acronyms
DIS= convertCharsToStrings(DIS); %Disclaimer source
PTCCondtion =name;
PTCC = PTCCondtion;
PTCxh=PTCC;
DA= "ref_color1";%Default Appearance
numFiles = numel(baseName);
chr1=Name;
chr2='.mtl';
for k=290
for i=1:3
% build filename % e.g. AL083Bar.txt
filename = append(chr1(i,1),chr2);
filepath = cd;
file = fullfile(filepath, filename);
fileID= fopen(filename,'w');
if fileID==-1
error('Cannot open file for writing: %s', file); %code to test if files broken
end
%code for the text in the file
formatSpec = 'ND_RelParSet_K01 = {\n\nName = %s\n\nPARAMETERS =\n{\n Name = PTC_MATERIAL_DESCRIPTION\n Type = String\n Default = ''%80s''\n Access = Full\n},\n{'
formatSpec= [formatSpec newline ' Name = TEMPERATURE\n Type = Real\n Default = 0.000000e+00 F\n Access = Full\n},\n{'];
formatSpec= [formatSpec newline ' Name = PTC_BEND_TABLE\n Type = String\n Default = ''''\n Access = Full\n},\n{'];
formatSpec=[formatSpec newline ' Name = PTC_MATERIAL_TYPE\n Type = Integer\n Default = 9\n Access = Locked\n},\n{'];
formatSpec=[formatSpec newline ' Name = PTC_FAILURE_CRITERION_TYPE\n Type = String\n Default = ''NONE''\n Access = Locked\n},\n{'];
formatSpec=[formatSpec newline ' Name = PTC_FATIGUE_TYPE\n Type = String\n Default = ''NONE''\n Access = Locked\n},\n{'];
formatSpec=[formatSpec newline ' Name = PTC_MATERIAL_SUB_TYPE\n Type = String\n Default = ''LINEAR''\n Access = Locked\n},\n{'];
formatSpec=[formatSpec newline ' Name = PTC_POISSON_RATIO\n Type = Real\n Default = %7f\n Access = Full\n},\n{'];
formatSpec=[formatSpec newline ' Name = PTC_YOUNG_MODULUS\n Type = Real\n Default = %7f psi\n Access = Full\n},\n{'];
formatSpec=[formatSpec newline ' Name = PTC_THERMAL_EXPANSION_COEF\n Type = Real\n Default = %7f /F\n Access = Full\n},\n{'];
formatSpec=[formatSpec newline ' Name = PTC_TENSILE_YIELD_STRESS\n Type = Real\n Default = %7f psi\n Access = Full\n},\n{'];
formatSpec=[formatSpec newline ' Name = PTC_TENSILE_ULTIMATE_STRESS\n Type = Real\n Default = %7f psi\n Access = Full\n},\n{'];
formatSpec=[formatSpec newline ' Name = PTC_MASS_DENSITY\n Type = Real\n Default = %7f lbm/in^3\n Access = Full\n},\n{'];
formatSpec=[formatSpec newline ' Name = MATL_TYPE\n Type = String\n Default = ''%s''\n Access = Full\n},\n{'];
formatSpec=[formatSpec newline ' Name = MATL_FORM_AND_SIZE\n Type = String\n Default = ''%s''\n Access = Full\n},\n{'];
formatSpec=[formatSpec newline ' Name = MMATL_SOURCE_DENSITY\n Type = String\n Default = ''%s''\n Access = Full\n},\n{'];
formatSpec=[formatSpec newline ' Name = MATL_SOURCE_POISSONS_RATIO\n Type = String\n Default = ''%s''\n Access = Full\n},\n{'];
formatSpec=[formatSpec newline ' Name = MATL_SOURCE_YOUNGS_MODULUS\n Type = String\n Default = ''%s''\n Access = Full\n},\n{'];
formatSpec=[formatSpec newline ' Name = MATL_SOURCE_TENSILE_YIELD\n Type = String\n Default = ''%s''\n Access = Full\n},\n{'];
formatSpec=[formatSpec newline ' Name = MATL_SOURCE_TENSILE_ULTIMATE\n Type = String\n Default = ''%s''\n Access = Full\n},\n{'];
formatSpec=[formatSpec newline ' Name = MATL_SOURCE_THREM_COND\n Type = String\n Default = ''%s''\n Access = Full\n},\n{'];
formatSpec=[formatSpec newline ' Name = MATL_SOURCE_THREM_EXP\n Type = String\n Default = ''%s''\n Access = Full\n},\n{'];
formatSpec=[formatSpec newline ' Name = MATL_SOURCE_NOTE_ADDITIONAL\n Type = String\n Default = ''%s''\n Access = Full\n},\n{'];
formatSpec=[formatSpec newline ' Name = MATL_SOURCE_NOTE_TEMP\n Type = String\n Default = ''%s''\n Access = Full\n},\n{'];
formatSpec=[formatSpec newline ' Name = MATL_DISCLAIMER\n Type = String\n Default = ''%s''\n Access = Full\n},\n{'];
formatSpec=[formatSpec newline ' Name = MATL_MANUFACTURE\n Type = String\n Default = ''%s''\n Access = Full\n},\n{'];
formatSpec=[formatSpec newline ' Name = MATL_GENERAL_USE\n Type = String\n Default = ''%s''\n Access = Full\n},\n{'];
formatSpec=[formatSpec newline ' Name = MATL_SPECIAL_FEATURE\n Type = String\n Default = ''%s''\n Access = Full\n},\n{'];
formatSpec=[formatSpec newline ' Name = MATL_ACRONYMS\n Type = String\n Default = ''%s''\n Access = Full\n},\n{'];
formatSpec=[formatSpec newline ' Name = MATL_DISCLAIMER_USES\n Type = String\n Default = ''%s''\n Access = Full\n},\n{'];
formatSpec=[formatSpec newline ' Name = MATL_SOURCE_DATE\n Type = String\n Default = ''%s''\n Access = Full\n}\n\n}\n'];
%if D==0 %density
%%if Y=num([3:end],15); %youngs
%%if TY=num([3:end],17); %tensile yeaild e+0x
%%if TU=num([3:end],19); %tensile ULT e+0x
%if TC=num([3:end],21);%thermal concuctivity e+0x
%if TEX=num([3:end],23);
fprintf(fileID,formatSpec,Name(i,1),DE(i,1),PR(i,1),Y(i,1),TEX(i,1),TY(i,1),TU(i,1),D(i,1),MT(i,1),FO(i,1),DS(i,1),PS(i,1),YS(i,1),TYS(i,1),TUS(i,1),TS(i,1),CS(i,1),AD(i,1),ST(i,1),DI(i,1),MA(i,1),GU(i,1),SF(i,1),AC(i,1),DIS(i,1),SD(i,1));
% write data to file
% writematrix(Formatspec,filename)
end
if PR==0;%trying to delet poisions line
fid = fopen( filename, 'r')
fgetl(fid)
end
end
fclose('all')
  1 Kommentar
Image Analyst
Image Analyst am 18 Nov. 2021
Bearbeitet: Image Analyst am 18 Nov. 2021
Is this the "if" statement you're referring to:
if PR==0;%trying to delet poisions line
fid = fopen( filename, 'r')
fgetl(fid)
end
First of all you need to accept the results of fgetl into a variable
thisLine = fgetl(fid);
Then you need to check the text line to see if it contains something you want to use and operate on, or if you want to skip doing anything with that line:
% Open the file for reading in text mode.
fileID = fopen(fullFileName, 'rt');
% Read the first line of the file.
textLine = fgetl(fileID);
lineCounter = 1;
while ischar(textLine)
% Print out what line we're operating on.
fprintf('%s\n', textLine);
% Read the next line.
textLine = fgetl(fileID);
% Skip lines that say "skip this line" or whatever.
if contains(textLine, 'skip this', 'IgnoreCase', true)
continue; % Skip to end of loop.
end
lineCounter = lineCounter + 1;
% If you get here then you want to do something with that line,
% like parse it to get values out of it or something...
end
% All done reading all lines, so close the file.
fclose(fileID);

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Mehr zu Entering Commands finden Sie in Help Center und File Exchange

Produkte


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by