Effacer les filtres
Effacer les filtres

How to write a function that returns a graph?

189 vues (au cours des 30 derniers jours)
Larry
Larry le 20 Fév 2014
Commenté : Emir Dönmez le 27 Fév 2023
If I write a function like
function A = myplot(x,y)
A = plot(x,y);
set(Aetc....)
end
When I call this function, a lot of numbers instead of a plot are shown. If modify the code as follows,
function A = myplot(x,y)
plot(x,y)
end
then an error occurs as output argument "A" is not assigned during the call to the function.
How may I correct this?

Réponse acceptée

Dishant Arora
Dishant Arora le 20 Fév 2014
your 2nd syntex shouldn't give any error, you need to not specify output argument while calling the function from command prompt:
a = myplot(x,y) % will result in error as a isn't defined
myplot(x,y) % will give you desired result
Moreover you also don't need to have any output arguments while defining the function:
function myplot(x,y)
%function body
end
  1 commentaire
Emir Dönmez
Emir Dönmez le 27 Fév 2023
On my project i wrote code and it works fine but i want to add a function to plot so ı'm trying to call my plot funciton without defining any output arguements. How can ı call it? I'm newbie, i basical expressions.
This is the function ı'm trying to call:
Main_path = "C:\Users\emird\AppData\Local\Programs\MatLab"
function plotter(files, Main_path)
Exceldir = Main_path + "\ExcelFiles";
cd(Exceldir); % Change current directory
for i=1:length(files)
% Get sheet names being in the excel file.
sheets = sheetnames(files(i).name); % Sheets array.
% Get whole datas inside the excel files month by month.
for j=1:length(sheets)
data = readtable(files(i).name, "Sheet", sheets(j));
days = 1:1:size(data.Date); % Days array.
hold on;
plot(data.number_sold, days, "b.", "LineWidth", 2) % xAxis = sold_number
% yAxis = days
end
end
hold off;
end

Connectez-vous pour commenter.

Plus de réponses (2)

Wayne King
Wayne King le 20 Fév 2014
You can simply not have an output argument
function myplot(x,y)
plot(x,y)
end
That's one way

Walter Roberson
Walter Roberson le 21 Fév 2014
Remember, the return value from a plot() call is a handle graphics handle (or vector of them.) The actual plot resides in graphics memory.

Catégories

En savoir plus sur 2-D and 3-D Plots dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by