How do I insert a picture into a script?

I need to have an image in my code for users to reference. I don't want to imread or imload, I just want an immage pasted almost like a comment. Off to the side and not affecting the code at all. Can I do that?

 採用された回答

Steven Lord
Steven Lord 2022 年 11 月 19 日

0 投票

If your script is a live script that you have open in the Live Editor, select the Insert tab of the Toolstrip then click the Image button.

4 件のコメント

DKonk
DKonk 2022 年 11 月 19 日
And if my script is NOT a live script? I'm writing a code that will be a beam calculator and output shear/moment diagrams (The purpose isn't very important). But when people use this code, I want them to be able to reference an image so they can put in appropriate lenghts, forces, moments etc.
Image Analyst
Image Analyst 2022 年 11 月 19 日
You could create a Word document or a PDF. Or maybe use publish
Steven Lord
Steven Lord 2022 年 11 月 19 日
This also works in live functions.
If you're asking if you can somehow embed a picture into a plain code file with the .m extension and have it automatically displayed in the Editor the answer is no. MATLAB program files with the .m are text files.
Depending on how much effort you want to put into this project, if this is part of a toolbox that you're writing you could create custom documentation.
Or you could create a simple app that allows users to enter the data then have the app call your computational function to compute the answer.
DKonk
DKonk 2022 年 11 月 19 日
This is unfortunately the answer I was looking for. The asnwer is no. Thanks!

サインインしてコメントする。

その他の回答 (2 件)

Hamid Farshi
Hamid Farshi 2026 年 2 月 15 日
移動済み: Walter Roberson 2026 年 2 月 15 日

1 投票

We have introduced a plain text .m based live script format, since 2025a release, which allows you to achieve this. The image will be stored for you at the bottom of your file. For more information on this format, please take a look at our documentation in here
Jan
Jan 2022 年 11 月 19 日
編集済み: Jan 2022 年 11 月 19 日

0 投票

It depends on how you would like to see the image. It cannot be displayed in the editor, because scripts are M-files and therefore pure text files. Of course ASCII-art would be possible.
If you want to display it by imshow you can embed base64 encoded data:
% Create the data stream as ASCII:
img = randi([0, 255], 20, 30, 'uint8'); % Example image
B64 = org.apache.commons.codec.binary.Base64;
str = char(B64.encode(img(:))).';
% Store in the file:
img64 = ['0Ocg6aEYR4z19yj49XzMJGvqyvWnCdnvrcG+ZKcrtAhGCxjSsVHzCH', ...
'Bhw8svfXKltcFGrqcpHn/1V5U5wEGBsuT1jCMmQddB0D7tWTJAnXla1JWM6', ...
'knBwGGREw2Hx+8hkXgDVinLT4cqmkOnsL9zFTrpJ9OJ/xRxG/YBxtHeFWZC', ...
'zG7pLkMlIt6UjCXan1mDZhM9Hy89agzn8X19VuZeHMdjPWcYIfH0kw88WtI', ...
'DCyumu6VzjEu+MK8uXqDHFO3GfG9yToKC0cukYM+IWfDgjJ+WNU14O9gxOS', ...
's6b0/sbi/n+nAcQmiYQ5q2OB5LUWyCFUPNB+26fZQ8dfaLhTt9n61lXvwJ4', ...
'unLGUNVriK4G6d+x7fn5FWyMge+gHrnnJ7czpMuPeIHfSr6toB4D64KEoUY', ...
'0dG4JqiE+abMdG7TFSIsZNTND2aGaqigSm4D+yobXzJ9VvPrDbxEbIzxavt', ...
'Ns6qKsqotIP8rCI/hqzBedfso26VgMG17HpY5YpVASp1D0/u6WJUb6OHRQp', ...
'gFbFApLWwYmXiys6MIEVGHp2jRt/eIUxucx2wXRCdHcIZ14ITxo/U9rUqrs', ...
'RFBOarYWMesAZpj6gB2bHXFUsh4CS24eSdXmzG9PupEwzBJF5Oui2ykpa2i', ...
'8TW1PB6bc3WpxVmpatfVQZ2Vit5DUR7wpXqji6WLuIX+NxscEGdyXcOgxe7', ...
'5MSOyGIaH3Hxkq72FWSaWQwvBPnGwW7xlrrRxBVRsRTLSbuNkxGXOwWA3yv', ...
'NTq3DVxCrc/YPiliczaL/TylGIFxwirX4wfiUO'];
B64 = org.apache.commons.codec.binary.Base64;
img = uint8(B64.decode(uint8(img64));
imshow(reshape(img, 20, 30));

カテゴリ

ヘルプ センター および File ExchangeEnvironment and Settings についてさらに検索

製品

リリース

R2022b

質問済み:

2022 年 11 月 19 日

移動済み:

2026 年 2 月 15 日

Community Treasure Hunt

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

Start Hunting!

Translated by