Add figure to .txt file using fprintf
8 views (last 30 days)
Show older comments
Justin Western
on 2 May 2019
Commented: Justin Western
on 30 May 2019
I'm writing my semester gpa and cumulative gpa to a .txt file for each semester for fun/records. I have it all done but decided I needed a plot to show the trends of my gpa. I was wondering if there was a way to write the figures into the .txt from matlab using fprintf.
2 Comments
Accepted Answer
Sulaymon Eshkabilov
on 14 May 2019
Hi Justin,
You can try this script with dlmwrite() for RGB images in jpg/jpeg/png... etc:
clearvars; close all
MM = imread('GPA.jpeg'); % Original image
R = MM(:,:,1); % Red color layer
G = MM(:,:,2); % Green color layer
B = MM(:,:,3); % Blue color layer
figure(1)
imshow(MM)
% Write the read image data (three layers) into (three) external *.txt files
dlmwrite('MY_R.txt', R, 'delimiter', '\t', 'precision', 0);
dlmwrite('MY_G.txt', G, 'delimiter', '\t', 'precision', 0);
dlmwrite('MY_B.txt', B, 'delimiter', '\t', 'precision', 0);
% Test: load and convert to uint8 integer format
RED = load('MY_R.txt');
GRE = load('MY_G.txt');
BLU = load('MY_B.txt');
MMnew(:,:,1)=uint8(RED);
MMnew(:,:,2)=uint8(GRE);
MMnew(:,:,3)=uint8(BLU);
% Compare the re-imported data with the original
figure(2)
imshow(MMnew)
This works quite well but a bit slow. Its processing time very much depends on the size/resolution of the image that you are working with.
Note that dlmwrite() is slow and not recommended to write matrices to ASCII-delimited file. Instead, writematrix() is recommended. But my MATLAB package does not have that function and thus, I can't state how far it is applicabale in your case.
Good luck.
More Answers (1)
Sulaymon Eshkabilov
on 13 May 2019
Hi,
It is possible to do it.
(1) If you shall save your plot as *.jpg, jpeg, *.tiff, *.... etc. image file;
(2) you shall read it via imread();
(3) now, you can export/write your read data in MATLAB into *.dat, *.txt, *.xls, ... etc file formats using fprintf(), dlmwrite(), xlswrite(), ...
Good luck.
See Also
Categories
Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!