Need a little help as I am complete Matlab novice. I need to write a program consisting of 2 functions and a main function that calls the other 2 functions. For a little background, this will calculate gear ratios and max speed at each gear for the new adjusted gear ratio. My first Function (GetData) will collect info from the user using a Gui which asks 7 different questions. In that same function my output arguments are used to calculate the new and stock gear ratios and the tire circumference from gathered data.
My 2nd function will use the data gathered from the GetData function and make all the calculations using the outputs from the GetData function (stockGr, newGr, Tire). This data will be applied to several other calculations in order to determine max speed at each gear.
The main function will call both functions and plot a graph.
My troubles are I have no idea how to take the outputs from GetData, call that function in my Calc function and use the output arguments in the necessary calculations. I know this is probably a terrible way to program but I just need to get it done and meet the requirements. Just looking for some guidance here. I think I am also hung up on what should be my input and output arguments in both the main function and the calling function.
Thanks

댓글 수: 4

James Tursa
James Tursa 2019년 4월 26일
편집: James Tursa 2019년 4월 26일
An example outline:
% Your main code goes here
[stockGR,newGR,Tire] = GetData; % call your input routine
[speedGR] = UseData(stockGR,newGR,Tire); % call your calculations
% plotting code goes here
function [stockGR,newGR,Tire] = GetData
% your function code goes here
% stockGR, newGR, and Tire are the outputs
end
function [speedGR] = UseData(stockGR,newGR,Tire)
% your function code goes here
% stockGR, newGR, and Tire are the inputs
% speedGR is the output
end
Or, instead of having multiple variables, you could use a struct variable with fields for each value. That way you only need to pass around one variable instead of many.
Flyers28
Flyers28 2019년 4월 26일
I'll spend some time and make sense of all that, like I said I am a complete novice, haha.
The way I was trying to do it was generate each function as a seperate .m File. Then use a main function to call the 2 other functions. But what your saying is write each function in the main function. I think I will stick with using many variables. Not sure what struct variables with fields are, haha.
Since I am using a GUI to gather data, does my GetData function even need any input arguments?
Thanks for the super quick response. I'll mess around with it this weekend and see what I can come up with.
dpb
dpb 2019년 4월 27일
No, James' code could just as easily be three separate m-files.
The GetData function doesn't have any input arguments only outputs (altho one alternate way to do user input would be to let the user edit a text file that is read rather than enter each variable manually -- in that case, it would make sense to pass the file name as an argument.
It could get awfully old quickly to have to set all seven input variables every time...might want to think about whether most of those seven have default values or other ways to minimize the amount of absolutely necessary user interaction.
Flyers28
Flyers28 2019년 4월 27일
Thanks guys, really appreciate the help. I think I got it to work. Next up is just learning a few more things on generating a plot exaclty how I want it. But you guys helped me with my biggest hurdle and for that I thank you again.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

도움말 센터File Exchange에서 Workspace Variables and MAT Files에 대해 자세히 알아보기

질문:

2019년 4월 26일

댓글:

2019년 4월 27일

Community Treasure Hunt

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

Start Hunting!

Translated by