Calling a Function From Another Function

I created a function called table(data) which returns a matrix with labels from the data. I would like to try and create another function which can use table(data). The reason I would like to learn this is because if I create a new function it will have to be by using just data again. I would like to use the new created data in future functions and scripts.
This is what I though I would work which it didn't.
function []=food(table(data))
However, that doesn't work because it returns unbalanced or unexpected parenthesis or bracket.

5 Kommentare

Stephen23
Stephen23 am 8 Dez. 2017
"However, that doesn't work because it returns unbalanced or unexpected parenthesis or bracket."
No, that is just the error message. That doesn't work because you invented some syntax that is not permitted by MATLAB. Try doing the introductory tutorials, which explain how to call functions correctly:
MATLAB has very easy to read documentation which is easy to search using your favorite internet search engine: you obviously did not read anything about how to define functions or call functions, because the syntax that you invented is not shown anywhere in the MATLAB documentation.
Note that learning any programming language by guessing is very inefficient and will just waste your time.
Rafael
Rafael am 8 Dez. 2017
I read that page, although it doesn't show me what I want to learn. Is not even including ''functions'' in their tutorial.
Greg
Greg am 8 Dez. 2017
@Rafael, sadly, you aren't wrong.
The statement "I created a function called table(data) which returns a matrix" confuses me.
T = table( data );
returns a table object, not a matrix. Thus I assumed that you had used the name table for your own function.
Stephen23
Stephen23 am 8 Dez. 2017
Bearbeitet: Stephen23 am 8 Dez. 2017
"I read that page, although it doesn't show me what I want to learn. Is not even including ''functions'' in their tutorial."
This is the first page returned when I searched for "MATLAB functions":
It took me 0.5 seconds to locate.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

per isakson
per isakson am 8 Dez. 2017
Bearbeitet: per isakson am 8 Dez. 2017

0 Stimmen

M = table_1( data );
food( M );
or
food( table_1( M ) );
The two line construct is easier to debug
where
function food( m )
% m is a numerical matrix
...
end

8 Kommentare

Rafael
Rafael am 8 Dez. 2017
The problem I am having with this is that I have a table that shows me this matrix. I created a function that organized the table and I also added a title to each column. The thing is that I would like to use that new generated table in another function that I want to create that has to use the table.
per isakson
per isakson am 8 Dez. 2017
Bearbeitet: per isakson am 8 Dez. 2017
table is just another data type - the syntax is the same. I edited my answer accordingly.
Here is the new function I want to use the new table, how can I write your code inside my new function?
function [Meat,Candy]=food(data)
clc
format compact
Food=input('Enter the food: ');
Candy=input('Enter the candy: ');
Index=find(data(:,1)==Food & data(:,16)==Candy);
fprintf('The food is: %d Calories \n',min(min(data(Index,2:13))))
fprintf('The food is: %d Calories \n',max(max(data(Index,2:13))))
end
per isakson
per isakson am 8 Dez. 2017
What do you mean by the word "table"?
is the old function which I created for the matrix.
function [ ]=table(data)
That's where the word table come from. The name of the function.
Stephen23
Stephen23 am 8 Dez. 2017
Bearbeitet: Stephen23 am 8 Dez. 2017
@Rafael: do not use table as the name of your function. It shadows the inbuilt function table, and confuses everyone because they think that you are talking about the table data type.
TIP: before using any function/variable name you should check if it is already used by calling which with that name.
Rafael
Rafael am 8 Dez. 2017
I changed it to table_1. Now, how can work with it on a new function.
per isakson
per isakson am 8 Dez. 2017
I changed my answer back to the original one, which shows the syntax you are asking for.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Deep Learning Toolbox finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 7 Dez. 2017

Kommentiert:

am 8 Dez. 2017

Community Treasure Hunt

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

Start Hunting!

Translated by