Using sprintf function?
2 次查看(过去 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!