Add figure to .txt file using fprintf

8 views (last 30 days)
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
KSSV
KSSV on 2 May 2019
figures you mean plots?
Justin Western
Justin Western on 13 May 2019
I assumed it was interchangable and didn't need clarification, but yeah plots.

Sign in to comment.

Accepted Answer

Sulaymon Eshkabilov
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.
  1 Comment
Justin Western
Justin Western on 30 May 2019
Hi Sulaymon,
Sorry it took so long but thank you that answered my questions!
thanks again

Sign in to comment.

More Answers (1)

Sulaymon Eshkabilov
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.
  1 Comment
Justin Western
Justin Western on 14 May 2019
This is what I tried:
semester = imread('GPA_Semester.tif');
cumulative = imread('Cumulative_GPA.tif');
fprintf(M,'%s',semester);
but when I try to open the *.txt file it says "Text encoding Unicode (UTF-8) isn’t applicable." I'm using a mac, could that be the issue, or maybe the fprintf() formatting operator is wrong?

Sign in to comment.

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!