Using sprintf function?
조회 수: 3 (최근 30일)
이전 댓글 표시
Hi All,
I have Excel file which has names of outputs. I want to save the output images as the same names in the excel file.
Thank you in advance
Here is my code
clear all, clc
cd C:\Calculated_NDVI_Arcgis
dinfo = dir('*_B3*.tif');
nfile = length(dinfo);
filenames = {dinfo.name};
for K = 1 : nfile
b3_file{K} = filenames{K};
band_pos{K} = strfind(b3_file{K}, '_B3');
b4_file{K} = b3_file{K}; b4_file{K}(band_pos{K} + 2) = '4';
b3_data{K} = double( imread(b3_file{K}) );
b4_data{K} = double( imread(b4_file{K}) );
finalndvi{K} = (b4_data{K} - b3_data{K}) ./ (b4_data{K} + b3_data{K});
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
finalndvi{K}=flipud(finalndvi{K});
R = georasterref('RasterSize',size(finalndvi{K}),'LatitudeLimits',[30.95115,31.76385],'LongitudeLimits',[46.58315,47.43285]);
[num,txt] = xlsread('NDVI_data_name.xls','Sheet1','A1:A2');
geotiffwrite(sprintf(txt.tif,K),finalndvi{K},R);
end
댓글 수: 2
Walter Roberson
2018년 1월 25일
"I have Excel file which has names of outputs. I want to save the output images as the same names in the excel file."
How is that intended to work? If the 5th file is EK1903BR_B3Q81.tif then how do you know which name in the xls file to use to write it?
답변 (1개)
Dan Klemfuss
2018년 1월 24일
Good evening Reyadh. I believe the issue that you're having with sprintf is that it needs to be formatted as a string. You currently have:
geotiffwrite(sprintf(txt.tif,K),finalndvi{K},R);
I believe that it needs to be modified to:
geotiffwrite(sprintf('%s.tif',txt),finalndvi{K},R);
This will assign the value of txt where the %s is. Please let me know if you have any questions!
댓글 수: 6
Jan
2018년 1월 25일
@reyadh Albarakat: It is still not clear. What do you expect as output of "sprintf(txt.tif, K)"? The command does not work and therefore the readers cannot guess, what you want it to do. "give names to output images" is not clear also. When "txt" is a cell array, containing the data of the Excel file, what to you expect "txt.tif" to contain? Which name do you want for the created images? "txt1.tif"?
참고 항목
카테고리
Help Center 및 File Exchange에서 Spreadsheets에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!