How to import text present in figure to excel?

Hello,
I wrote text in figure.For example:
figure
axis off
text(0,0.5,sprintf([ 'speed = %0.1f kmph (%0.1f miles/hour)\n'
'time = %0.1f min \n '
],speed,time;
And the output:
speed= 55kmph(30miles/hour)
time= 35min
How want import this output to excel?
Thank you in advance

2 Kommentare

Rik
Rik am 28 Jan. 2022
Do you want to export the text as text? Or the figure as an image?
Hello Rik,
I want to export the text as text.
I want to see below output in the excel.
speed= 55kmph(30miles/hour)
time= 35min
Thank you

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Ankit
Ankit am 28 Jan. 2022
There is one way to write the output to *.xlsx-File.
speed= 55;
time= 35;
filename = 'Test.xlsx';
str = sprintf(['speed = %0.3f kmph \n' 'time = %0.1f min \n '],speed,time);
text(0,0.5,str);
OutPut = cellstr(str);
writetable(cell2table(OutPut),filename);

12 Kommentare

Hello Ankit,
I got this error. when I used cellstr
"Conversion to cellstr from matlab.graphics.primitive.Text is not possible"
Output=cellstr(str);
writetable(cell2table(Output),Book1.xls);
I used writematrix
writematrix(str,'Book1.xlsx','Sheet',4);
winopen('Book1.xlsx');
It worked but I'm seeing the data horizontally in excel. Like this.
speed= 55kmph(30miles/hour)time= 35min
But I want to see like this
speed= 55kmph(30miles/hour)
time= 35min
how to see the data vertically?
Thnak you in advance.
Ankit
Ankit am 31 Jan. 2022
Bearbeitet: Ankit am 31 Jan. 2022
hi for me it is working fine. I didn't use writematrix before.Did you got this from File Exchange?
For writing to xls-File I prefer writetable command. see below result I am getting as per above solution.
can you post the code that you are using and also the error ?
Rik
Rik am 31 Jan. 2022
The writematrix function was introduced in R2019a. It is intended as a replacement for xlswrite.
Ankit
Ankit am 31 Jan. 2022
Thanks @Rik I am still using 2018b and planning to switch to higher version
Hello Ankit,
When I tried to use writetable.
text(0,0.5,str);
writetable(str,'Book1.xlsx');
winopen('Book1.xlsx');
But it is showing the below error:
Error using writetable (line 24)
Unsupported type 'char'. Use writematrix instead.
Error in script (line 221)
writetable(str,'Book1.xlsx');
Any idea why it is working for you and not for me?
Thanks!
Ankit
Ankit am 2 Feb. 2022
Above code you posted definitely throws an error if you str is not in table format.
When you are writing to xlsx using writetable the str has to be in table format thats why i used cell2table(str)
cellstr i used to convert into cell. Could you please tell me what is the type (cell, char etc) of your str?
Hello Ankit,
The format of str is in the below format.
str=text(0,0.5,sprintf([ ...
'Distance = %0.1f km (%0.1f miles)\n' ...
'Driving time = %0.1f min \n ' ...
'Avg speed = %0.1f kmph (%0.1f mph)\n ' ...
],DistanceTraveled,DistanceTraveled/1.609, Driving_time,Average_speed),'FontSize',14)
Your code will make str to return text object
str=text(0,0.5,sprintf([ ...
'Distance = %0.1f km (%0.1f miles)\n' ...
'Driving time = %0.1f min \n ' ...
'Avg speed = %0.1f kmph (%0.1f mph)\n ' ...
],DistanceTraveled,DistanceTraveled/1.609, Driving_time,Average_speed),'FontSize',14)
DistanceTraveled = 1;
Average_speed = 1;
Driving_time = 1;
filename = 'Test.xlsx';
str = sprintf([ ...
'Distance = %0.1f km (%0.1f miles)\n' ...
'Driving time = %0.1f min \n ' ...
'Avg speed = %0.1f kmph (%0.1f mph)\n' ...
],DistanceTraveled,DistanceTraveled/1.609, Driving_time,Average_speed);
text(0,0.5,str,'FontSize',10);
OutPut = cellstr(str);
writetable(cell2table(OutPut),filename);
Rik
Rik am 3 Feb. 2022
If the goal is to write something to an excel file, why is the text function used at all? That is an unnecessary step that still confuses me.
Ankit
Ankit am 3 Feb. 2022
Yes one can avoid that step but it is not clear how he is getting the data. I just used the def values.. whether he is displaying that data in figure etc.. if not then text command is not required..

Melden Sie sich an, um zu kommentieren.

Gefragt:

am 27 Jan. 2022

Kommentiert:

am 3 Feb. 2022

Community Treasure Hunt

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

Start Hunting!

Translated by