Is it possible to run a function that requires input variables when they are declared later in the function?

23 次查看(过去 30 天)
I am interested in running a function that will plot a few different variables against eachother depending on which ones I input as the function input. However, these variables are the outputs of another function, or rather they are defined later in the function, AFTER I say they are the ones that I want to plot. In the past, I have ran the first function to put the variables in the 'base' workspace, then ran the second function which uses those variables.
The first fucntion looks like this (I've changed names to make it simpler to understand):
function [array_xldata] = grab_xldata ()
% Pull data from excel file
table_xldata = readtable('xl_sheet.xlsx','Sheet',1,'Range','K1:K18');
% Convert data from table to array
array_xldata = table2array(table_xldata);
% Send data out of local function to workspace to be used by other functions
assignin('base','array_xldata',array_xldata);
end
And then the second function would use that variable, array_xl_data, as an input, pulling it from the workspace. I want to not use the workspace like this, and instead be able to keep it nice and tidy, and not have to run two seperate functions because at that point I should just combine them into one function. If I do not run the first function first in order to grab the data and put it into the workspace, when I enter the variable array_xl_data as the input of the second function, it says that the variable is undefined. So, I tried making the second function call on the first function BEFORE it tries to use the variable from the first function.
The second function looks like this (I've changed names to make it simpler to understand):
function [] = sortandplot(x_name)
% First function is used to pull data from excel file and put it into workspace
grab_xldata();
% Use data from first function in this function
x_name = array_xldata;
% Sort data
x_sorted = sortrows(x_name);
% Plot data
plot(x_name,x_name);
end
This is supposed to run the first function, which will collect the data and publish it to the workspace as the variable array_xldata, and then pull that data from the workspace and use it in the function. But it isn't working, and just says the function array_xldata is undefined, even though it will be defined (or so I think) if the code would run that first function before doing anything else.
Is there any way to accomplish this, am I on the right track if so, and if not how should I be thinking of accomplishing this instead?
Also I'm fairly new to MatLab, sorry in advance!

采纳的回答

David K.
David K. 2019-7-26
It looks to me that the main problem is that you are using the first function wrong. When you define the function
function [array_xldata] = grab_xldata ()
you are saying that when this function is called it will return array_xldata. Which means, to get this data in your second function, you should actually be calling it like this:
x_name = grab_xldata();
This way, you do not need the assignin command in the first function at all.
Also, If I remember correctly, A function has its own workspace separate from the base workspace, so when you call the first function in the second, it assigns the data properly to the base workspace but the second function cannot acces the base workspace anyways.

更多回答(1 个)

Jon
Jon 2019-7-26
编辑:Jon 2019-7-26
You just need to give your first function a return argument when you call it.
So the definition of your first function should look something like this
function array_xldata = grab_xldata (filename)
% filename is the name of the excel file you want to read e.g.'xl_sheet.xlsx'
% Pull data from excel file
table_xldata = readtable(filename,'Sheet',1,'Range','K1:K18');
% Convert data from table to array
array_xldata = table2array(table_xldata);
end
then in your second function, call it assigning the return argument to array_xldata
function [] = sortandplot(filename)
% First function is used to pull data from excel file and put it into workspace
x_name = grab_xldata(filename);
% Sort data
x_sorted = sortrows(x_name);
...
end
Note that the name of the left hand (return argument) in the function definition does not need to be the same as the one you use when you call it. So in the defintion I used the variable named array_xldata and then when I called it I used x_name
Also note your last line of code in your example
% Plot data
plot(x_name,x_name);
doesn't make any sense, unless you just want a straight line of x_name plotted against itself. I presume you probably want to plot some column of x_name against another column for example
% Plot data
plot(x_name(:,1),x_name(:,2));
p.s. By the time I posted this I saw that you already had another answer from David K. which also correctly identifies the key problem as your needing to have a left hand argument when you call your first function. In case the additional details I provided are helpful I'll leave my answer up here for you to read.
  1 个评论
Stephen Rosa
Stephen Rosa 2019-7-26
Yes, you are right I did want to do multiple pieces of data, but I left that code out to make it simpler to understand, and plotted the same set of data against itself just to represent how I was using the variables. Thank you so much though, this helps a bunch!

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品


版本

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by