필터 지우기
필터 지우기

How can I constrain a plot on an excel sheet?

조회 수: 1 (최근 30일)
Christian Esposto
Christian Esposto 2021년 5월 4일
답변: Shivam Lahoti 2024년 2월 28일
Hello,
I created an excel add-in that outputs a plot, but i'd like it to be constrained on a certain region of the sheet, instead of floating around; is there a way to do it?
many thanks

답변 (1개)

Shivam Lahoti
Shivam Lahoti 2024년 2월 28일
Hi Christian,
To ensure your chart remains within a specified area on the Excel worksheet, you can define its position and dimensions using properties such as Left, Top, Width, and Height within your add-in's code. Here's a concise MATLAB code example:
% Create a new instance of Excel
excelApp = actxserver('Excel.Application');
% Add a new workbook
workbook = excelApp.Workbooks.Add;
sheet = workbook.ActiveSheet; % Reference the active sheet
% Here you would typically run some MATLAB code that generates data for the plot
% For example, let's assume you have some X and Y data:
X = 1:10;
Y = rand(1, 10);
% Plot the data using MATLAB's plotting functionality
figure;
plot(X, Y);
% Copy the plot to the clipboard
print('-dmeta');
% Paste the plot from the clipboard into the Excel sheet as a chart object
sheet.Paste;
chartObject = sheet.Shapes.Item(sheet.Shapes.Count);
% Now, set the position of the chart within the sheet
% (The units are in points; 72 points = 1 inch)
leftPosition = 100; % For example, 100 points from the left edge of the sheet
topPosition = 50; % For example, 50 points from the top edge of the sheet
% Set the size of the chart
width = 300; % Width in points
height = 200; % Height in points
% Apply the position and size to the chart
chartObject.Left = leftPosition;
chartObject.Top = topPosition;
chartObject.Width = width;
chartObject.Height = height;
% Make Excel visible
excelApp.Visible = true;
You can adjust the Left, Top, Width, and Height values to fit your specific requirements. And refer to the above example for your plugin.
I hope it was helpful.
Regards,
Shivam.

카테고리

Help CenterFile Exchange에서 Data Import from MATLAB에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by