How do I define a variable from another function?

7 views (last 30 days)
I have a mulifuction script that is suppposed to ask the user for 4 different cars and weigh them based on ratings to give the user the best car to purchase.
What I want to do is have a prompt for every car the user inputs so the user can put in data for each variable the user decides to use. However, when titling the prompt I want to use the cars name in the prompt. It seems impossible to me and Im not sure what to do, im very new to coding.
This is the main script:
prompt1 = {'How Many Cars (4): '};
title1 = 'Cars';
answer1 = inputdlg(prompt1, title1, [1 40]);
Q1 = str2double(answer1{1});
[N] = Group_Function1(Q1);
Car1 = N(1); %Stores the names of the cars
Car2 = N(2);
Car3 = N(3);
Car4 = N(4);
prompt2 = {'How Many Variables (4): '};
title2 = 'Variables';
answer2 = inputdlg(prompt2, title2, [1 50]);
fprintf('This code can accept costs between 0-100000\n');
fprintf('This code can accept top speeds between 0-200\n');
fprintf('This code can also accept the terms none, some, & alot\n');
fprintf('This code can accept safety ratings between 0-5\n');
Q2 = str2double(answer2{1});
[V,W] = Group_Function2(Q2);
W1 = W(1); %Stores the weights of the varibles
W2 = W(2);
W3 = W(3);
W4 = W(4);
for h=1:Q1
[H] = Group_Function3(V);
Weights(h,:)=H;
end
Cost1 = str2double(Weights{1,1}); %stores the costs form the matrixs
Cost2 = str2double(Weights{2,1});
Cost3 = str2double(Weights{3,1});
Cost4 = str2double(Weights{4,1});
Topspeed1 = str2double(Weights{1,2}); %stores the costs form the matrixs
Topspeed2 = str2double(Weights{2,2});
Topspeed3 = str2double(Weights{3,2});
Topspeed4 = str2double(Weights{4,2});
MPG1 = str2double(Weights{1,3});
MPG2 = str2double(Weights{2,3});
MPG3 = str2double(Weights{3,3});
MPG4 = str2double(Weights{4,3});
Safety1 = str2double(Weights{1,4});
Safety2 = str2double(Weights{2,4});
Safety3 = str2double(Weights{3,4});
Safety4 = str2double(Weights{4,4});
CO1 = Cost1/10000;
CO2 = Cost2/10000;
CO3 = Cost3/10000;
CO4 = Cost4/10000;
TS1 = Topspeed1/20;
TS2 = Topspeed2/20;
TS3 = Topspeed3/20;
TS4 = Topspeed4/20;
Sa1 = Safety1*2;
Sa2 = Safety2*2;
Sa3 = Safety3*2;
Sa4 = Safety4*2;
COST = [CO1, CO2, CO3, CO4];
TOPSPEED = [TS1, TS2, TS3, TS4];
MPG = [MPG1, MPG2, MPG3, MPG4];
SAFETY = [Sa1, Sa2, Sa3, Sa4];
C1F = (CO1*W1)+(TS1*W2)+(Ca1*W3)+(Sa1*W4);
C2F = (CO2*W1)+(TS2*W2)+(MPG2*W3)+(Sa2*W4);
C3F = (CO3*W1)+(TS3*W2)+(MPG3*W3)+(Sa3*W4);
C4F = (CO4*W1)+(TS4*W2)+(MPG4*W3)+(Sa4*W4);
if C1F>C2F && C1F>C3F && C1F>C4F
disp(Car1)
fprintf('is the best car\n');
end
if C2F>C1F && C2F>C3F && C1F>C4F
disp(Car2)
fprintf('is the best car\n');
end
if C3F>C1F && C3F>C2F && C3F>C4F
disp(Car3)
fprintf('is the best car\n');
end
if C4F>C1F && C4F>C2F && C4F>C3F
disp(Car4)
fprintf('is the best car\n');
end
and heres the first function that asks for the vehicles names:
function [N] = Group_Function1(Q1)
%prompts fo name of cars
for Q = 1:Q1
prompt = {'Name of Car:'};
title = 'Car Name';
answer = inputdlg(prompt,title, [1 80])';
N(Q) = answer(1);
end
Now I ask for the variables names and weights:
function [V,W] = Group_Function2(Q2)
%prompts for variables and weights then stores both
for Q=1:Q2
prompt = {'Variable? (Negative Variables First):','weights in decimal form?'};
title = 'Variables and Weights';
answer = inputdlg(prompt,title, [1 80])';
V(Q)=answer(1);
W(Q)=str2double(answer{2});
s=sum(W);
end
if s~=1
fprintf('Weights do not add up to 1. Try Again!\n');
Group_Function2(Q2);
end
end
I want the answers from my first function to show up in the title for this functions prompts "title" so I know for which vehicle I am entering data for:
function [H] = Group_Function3(V)
%changes words to numbers
prompt = {V};
title = ['Variable Ratings For' Group_Function1(answer{1})];
h = inputdlg(prompt, title, [1 80])';
end
Anything helps, thank you

Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!