Output variable of a function as an input of another function - Problem

1 回表示 (過去 30 日間)
Christos Tsallis
Christos Tsallis 2021 年 3 月 25 日
コメント済み: Christos Tsallis 2021 年 3 月 25 日
Hello,
I got two ".m" files, the first one is called "Ex_1" and the second one is called "Ex_2" as shown bellow. Output of Ex_1 must be the Input of Ex_2. How can I make function Ex_2 understand the values of a, b? Is there anyway to call variable a and b from Ex_1 in Ex_2? Thanks for your time, appreciate your help!
function [a,b]=Ex_1(dat) %dat is just a variable which I load in my program
x=log10(dat(:,1));
y=dat(:,2);
P=size(dat,1);
SSxy=sum(x.*y)-(sum(x)*sum(y)/P);
SSxx=sum(x.^2)-((sum(x))^2/P);
a=SSxy/SSxx
y_bar=sum(y)/P;
x_bar=sum(x)/P;
b=y_bar-a*x_bar
end
function [freq]=Ex_2(a,b,db) %a, b is the same a, b of function Ex_1. db value doesn't matter
freq=(db-b)/a;
end
  2 件のコメント
David Hill
David Hill 2021 年 3 月 25 日
I don't understand your question. You are currently providing a,b,and db as inputs to function Ex_2. Why have two functions? Why not just provide Ex_1 with dat and db?
Christos Tsallis
Christos Tsallis 2021 年 3 月 25 日
編集済み: Christos Tsallis 2021 年 3 月 25 日
After I run Ex_1, I am typing Ex_2(a,b,0) and I am expecting the program to understand that a, b are the same values of Ex_1 (db=0 for this example). The problem is that I got an error of unrecognized function of variable 'a' that's why I am asking. I am trying to understand how functions call each other thats why I am using 2 functions.

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

採用された回答

drummer
drummer 2021 年 3 月 25 日
Did you do a third procedural script calling both of them?
You could do like this in your third script.m:
[a, b] = Ex_1(data);
%% % by doing this break, you can check if a and b were loaded into your workspace
% once they were loaded, you can now call your second script by
[freq] = Ex_2(a, b, db); % assuming you´re loading db variable mannualy.
This is not necessarily the only thing you could do. You could also type these lines in your command window.
  1 件のコメント
Christos Tsallis
Christos Tsallis 2021 年 3 月 25 日
Thanks for your help ! I am new to programming and I am facing problems with simple things.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by