MATLAB Answers

Imwrite with changing title

2 views (last 30 days)
Hi there, I am looking for help with using the imwrite function.
I have a string that changes when I put different values in the function.
[xCentroid,yCentroid]= Centroid(Beam)
ImLabel = sprintf("a= %.2f,b= %.2f,x= %.2f,y= %.2f,xBar= %.2f,yBar= %.2f,amplitude= %.2f,xCentroid: %.2f, yCentroid: %.2f",a,b,x,y,xBar,yBar,amplitude,xCentroid,yCentroid)
This creates this string: "a= 70.00,b= 10.00,x= 300.00,y= 300.00,xBar= 150.00,yBar= 150.00,amplitude= 250.00,xCentroid: 149.32, yCentroid: 150.18"
The numbers change how I want them to.
How do I have it so that whenever I run the program it saves in a folder? I have tried adding .jpg to the end of this string. No changes.
The rest of the code is as follows:
img = getframe(gcf);
fullFileName = fullfile(folder,ImLabel);
imwrite(img.cdata,fullFileName,'jpg')
At the moment it doesnt work unless I change ImLabel to something simple eg 'Test.jpg'.
Cheers,
Ollie

Accepted Answer

yanqi liu
yanqi liu on 5 Feb 2021
use
ImLabel = sprintf("a=%.2f_b=%.2f_x=%.2f_y=%.2f_xBar=%.2f_yBar=%.2f_amplitude=%.2f_xCentroid=%.2f_ yCentroid=%.2f",a,b,x,y,xBar,yBar,amplitude,xCentroid,yCentroid)

More Answers (0)

Community Treasure Hunt

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

Start Hunting!