how to use strtrim in loop

2 Ansichten (letzte 30 Tage)
Clayton Isherwood
Clayton Isherwood am 9 Feb. 2017
I am trying to use the function strtrim in the code below. It will perform the function on the first loop but the does not perform the function on the following loops.
function BarCodeParse
% filename = uigetfile('*.txt');
% =======================Variables to be written from txt input file to csv file==================
Model = char(100);
fileID = -1;
errmsg = '';
while fileID < 0
disp(errmsg);
% filename = input('Open file: ', 's');
filename = 'C:\Users\cisherwo\Documents\MATLAB\in.txt';
[fileID,errmsg] = fopen(filename);
end
fileID2 = -1;
while fileID2 < 0
disp(errmsg);
filename2 = 'C:\Users\cisherwo\Documents\MATLAB\out.csv';
[fileID2,errmsg] = fopen(filename2,'w');
end
%prompt={'Enter Name of output file'};
%prompt={'c:\temp\in.txt'};
%dlg_title = 'Input';
num_lines = 1;
%filename2=inputdlg(prompt,dlg_title);
tline = fgetl(fileID);
i=1;
fwrite(fileID2,['Model,' 'Part #,' 'Lot #,' 'Grid Resistance,' 'TC of Gage Factor,' 'Thrm Coeff 0,' 'Thrm Coeff 1,' 'Thrm Coeff 2,' 'Thrm Coeff 3,' 'Thrm Coeff 4,' 'Thrm Coeff 5,' 'Gauge Factor1,' 'Trans Sens 1,' 'GF 2,' 'Trans Sens 2,' 'GF 3,' 'Trans Sens 3,' 13 10 ],'char');
while ischar(tline)
if not(isempty(tline))
Model=tline(1,2:32);
s1=Model(1,1:2);
s1='4W';%=====for troublshooting
strcmp(s1,'4W');
if strcmp(s1,'4W')==1
PartNum=tline(1,308:325);
Lot=tline(1,36:49);
GridRist=tline(1,49:55);
TCoGF=tline(1,121:124);
ThermCo0=tline(1,145:153);%all thermal coef are for deg F
ThermCo1=tline(1,154:161);
ThermCo2=tline(1,162:169);
ThermCo3=tline(1,170:177);
ThermCo4=tline(1,178:185);
ThermCo5=tline(1,186:193);
GF1=tline(1,65:69);
GF2=tline(1,73:77);
GF3=tline(1,81:85);
GFTS1=tline(1,89:93);
GFTS2=tline(1,97:101);
GFTS3=tline(1,105:109);
i=i+1;
dataArray=[strtrim(Model),strtrim(PartNum),strtrim(Lot),strtrim(GridRist)];
fwrite(fileID2,[dataArray 13 10],'char');
end
end
tline = fgetl(fileID);
end
fclose(fileID);
fclose(fileID2);
These are the results
Model,Part #,Lot #,Grid Resistance,TC of Gage Factor,Thrm Coeff 0,Thrm Coeff 1,Thrm Coeff 2,Thrm Coeff 3,Thrm Coeff 4,Thrm Coeff 5,Gauge Factor1,Trans Sens 1,GF 2,Trans Sens 2,GF 3,Trans Sens 3,
4WK-13-062AP-350/WMMF007942K88FF07350.0
% This is the result I want for all lines of text.
4WK-13-125AD-350/W            MMF004107        K88FF00       350.0
4WK-13-125TM-350/W            MMF021197        K88FF00       350.0
0CEA-13-125UN-350MF003133 yA62AF873350.0
Thank you Clayton
  2 Kommentare
dpb
dpb am 9 Feb. 2017
I'm quite certain strtrim functions as it's intended on each and every call; undoubtedly the input to it isn't what you think it is for the cases in question.
It appears that the problem probably is the output formatting based on the look of the first two lines in the desired result section of the post; when you concatenate to create the variable dataArray in the loop that's going to be the stuff in the variables all strung together with no white space just like the first line in the results appears.
What is the input and what is the actual output wanted, precisely?
I'd suspect if you just set a breakpoint in the debugger at the beginning of the loop and step through a few records' processing the logic error will stand out pretty quickly where you're really not doing what you intend.
Unfortunately, from the posting as is we can't really tell what that intent is, exactly, and certainly can't infer it when don't know what the inputs were.
Clayton Isherwood
Clayton Isherwood am 9 Feb. 2017
I added the input file in case you wanted to look at it. We wanted to create a function that would take this input file and output a csv file.
We found the problem. The input text file contained hidden characters...so the function was working on every iteration. It would trim up to the hidden characters.
Thank you for your reply.
Clayton

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Guillaume
Guillaume am 9 Feb. 2017
strtrim removes surrounding spaces whereas it looks like you want spaces added.
It's not clear from your post whether each field should have fixed with. If they do, use fprintf with a fixed field width. You should be using fprintf instead of fwrite in any case:
fprintf(fileID2, '% 30s% 20s% 10s% 5s\r\n', Model, PartNum, Lot, GridRist) %for fixed width fields
Or if you want a fixed delimiter between each field:
fprintf(fileID2, '%s\t%s\t%s\t%s\r\n', , Model, PartNum, Lot, GridRist) %tab delimiter between each field

Kategorien

Mehr zu Statistics and Machine Learning Toolbox finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by