Using sprintf function?

3 views (last 30 days)
reyadh Albarakat
reyadh Albarakat on 24 Jan 2018
Commented: reyadh Albarakat on 25 Jan 2018
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 Comments
Walter Roberson
Walter Roberson on 25 Jan 2018
"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?
reyadh Albarakat
reyadh Albarakat on 25 Jan 2018
Hi Walter,
Each input of two images produces a single output image. I need to give 1st output image the 1st name from the names list which is available in the Excel file. And then 2nd output image takes the 2nd name from excel then 3rd and 4th ... etc.
(I have 20 input images so the output should be 10 all these 10 output images should take their names from Excel)
I have Attached the Excel file

Sign in to comment.

Answers (1)

Dan Klemfuss
Dan Klemfuss on 24 Jan 2018
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 Comments
Jan
Jan on 25 Jan 2018
@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"?
reyadh Albarakat
reyadh Albarakat on 25 Jan 2018
Dear Jan,
Please leave txt and leave the the excel file. Now i want to give names to output images Here is my code ( I removed this line (([num,txt] = xlsread('NDVI_data_name.xls','Sheet1','A1:A2'); sprintf(txt.tif, K)))
Here is my code
if true
% 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]);
geotiffwrite(sprintf('%d.tif',K),finalndvi{K},R);
end

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!