Help for setting the formatSpec

C
Cong Thien Dang · Oct 23, 2021 · 2K views
Question
Hi everyone,   I'm trying to read a picture file and get the size of the picture and print it to txt file. I want it like:   size img1.png 2448 3264 3 size img2.png 2458 3464 3 ........ But i don't know how to do it, please help me with the code.   Here my code:   for k = 1: length(jpgFiles) baseFileName = jpgFiles(k).name; fullFileName = fullfile(Folder, baseFileName); fprintf(1, 'Reading %s\n', fullFileName); imageArray = imread(fullFileName); E=size(imageArray); fid=fopen('Result.txt','a'); fprintf(fid,'%.d\n',E); fclose(fid);  
Expert Answer
Profile picture of John Michell
John Michell PhD Expert
Answered Aug 25, 2026
 fprintf(fid,'%d     %d     %d\n', E); 

It is much faster to open the file once only:

 fid = fopen('Result.txt','a');
 for k = 1:length(jpgFiles)
    baseFileName = jpgFiles(k).name;
    fullFileName = fullfile(Folder, baseFileName);
    fprintf(1, 'Reading %s\n', fullFileName);
    imageArray = imread(fullFileName);
    E = size(imageArray)   
    fprintf(fid, 'size %s\n', baseFileName);  % [EDITED]
    fprintf(fid, '%d     %d     %d\n', E);
 end
 fclose(fid);

 

100% Run Guarantee 3-Hour Fast-Track Delivery

Need a Custom Version or Complete Simulation for This Problem?

Our 500+ PhD engineers build, debug, and optimize working MATLAB scripts and Simulink (.slx) models tailored to your exact assignment rubrics with zero plagiarism.

Tested on MATLAB R2024b / R2026a
Turnitin 0% Plagiarism Report
Free 7-Day Revisions Guarantee
Have a different question? Ask here

Get a Free Consultation or a Sample Assignment Review!