How can I write data from a for loop to following excel sheet columns?

12 Ansichten (letzte 30 Tage)
RB
RB am 24 Sep. 2018
Kommentiert: Nicole Peltier am 26 Sep. 2018
Hello! Right now I have a code that reads a .tif file. The code then finds certain data about the picture (this is all working okay) and then inputs that data into an excel sheet. Each .tif file should produce three columns of data, which the code labels as area, minor, and major. Furthermore, I have input a second header that contains the .tif file name. I then made the code so that it could run through every .tif file in my directory. I want to code to be able to input the for loop data in incrementing excel columns. For example, the three column datas from the first .tif will be in columns A-C in the excel sheet, then the second .tif file data will go in D-F, and so on. The code seems to work with everything except for the fact that it is only inputting the final .tif file data and repeating it. For example, if I have 3 .tif files the output is an excel sheet with 9 columns that contain only the data from the last .tif file (3 columns of data repeated three times because the for loop iterated three times). I want to fix this problem so that columns 1-3 are for the first .tif, 4-6 are for second .tif, and 7-9 are for the third. How can I get rid of this repeat? My code is pasted below, and any help is greatly appreciated, thank you so much!
if true
files = dir('*.tif')
for file = files'
tiff_info = imfinfo(file.name);
tiff_stack = imread(file.name, 1) ;
for ii = 2 : size(tiff_info, 1)
temp_tiff = imread(file.name, ii);
tiff_stack = cat(3 , tiff_stack, temp_tiff);
end
AA={};
BW = tiff_stack;
BW(BW==0)=1;
BW(BW==10)=1;
BW(BW==170)=1;
BW(isnan(BW))=0;
BW(BW==20)=0;
BW(BW==30)=0;
BW(BW==40)=0;
BW(BW==50)=0;
BW(BW==60)=0;
BW(BW==70)=0;
BW(BW==80)=0;
BW(BW==90)=0;
BW(BW==100)=0;
BW(BW==110)=0;
BW(BW==120)=0;
BW(BW==130)=0;
BW(BW==140)=0;
BW(BW==150)=0;
BW(BW==160)=0;
CC = bwconncomp(BW);
stats = regionprops(CC, 'Image'); %NumberofBlobs by 1 array with pixels of each blob
[labeledImage, numberOfBlobs] = bwlabel(BW);
minor = regionprops(CC, 'MinorAxisLength');
major = regionprops(CC, 'MajorAxisLength');
area = regionprops(CC, 'Area');
minor = struct2cell(minor);
minor = minor';
major = struct2cell(major);
major = major';
area = struct2cell(area);
area = area';
tempAA ={area, minor, major};
% TableofBlobs = cell2mat(TableofBlobs);
tempAA = [tempAA{:}];
AA = [AA;tempAA];
header = {'area', 'minor', 'major'};
header2 = {file.name '' ''};
AA = [header;header2;AA];
filename = 'LamellasheetSizeData2.xlsx';
% AA=table(AA);
NumberFiles = length(files);
NumberColumns = NumberFiles.*3;
end
for n = 1:3:NumberColumns
c=xlsColNum2Str(n); %this is a defined function that translates the number into the corresponding excel column (i.e. n=2 translates to B and n=1 translates to A)
c=cell2mat(c);
% writetable(AA,'LamellaSheetSizeData.xls','Sheet',1,'Range',c
xlswrite('LamellaSheetSizeData',AA, 1, c);
end
end

Antworten (1)

Nicole Peltier
Nicole Peltier am 24 Sep. 2018
I'm pretty sure at least part of it is that you declare AA inside the for-loop through the files. Each time the loop runs, it empties out AA.
Also, vertically concatenating AA ( AA = [AA;tempAA]; ) will keep the data structure with only 3 columns. You will want to concatenate with a comma instead of a semicolon. If the length of tempAA varies between tif files, you won't be able to do this. If that's the case, you'll want to write the three columns from each tif file into the Excel spreadsheet individually (i.e., put xlswrite(...) inside the large for-loop).
Hope this helps!
  2 Kommentare
RB
RB am 25 Sep. 2018
Thanks for the help however I am still having issues! I tried horizontally concatenating by doing AA = [AA,tempAA]. I am not getting any error messages because the length of tempAA is not varying between the .tif files. Then I tried to also horizontally concatenate at the line AA=[header;header2;AA] by changing the semi colon to a comma. When doing this, I got the error message
if true
Error using horzcat
Dimensions of matrices being concatenated are not consistent.
end
Also, I tried mvoing around my xlswrite inside the different for loops, however this command is already inside the big for loop that runs through the three .tif files, so I am not sure what you mean when you say write the three columns from each tif file into the sheet individually. Any ideas about what I am doing wrong? Thanks again for your help!
Nicole Peltier
Nicole Peltier am 26 Sep. 2018
You would not want to horizontally concatenate the header onto the data because you want the header to be above each of the columns of data. Since you need a different header for each tif file, I would recommend rearranging some of your lines of code as follows:
header = {'area', 'minor', 'major'};
header2 = {file.name '' ''};
tempAA = [header; header2; tempAA];
AA = [AA, tempAA];
This way, you're attaching the header to the data before attaching one file's data to the big collection of data in AA.

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by