如何从另一个函数定义变量?

时间:2019-04-22 19:08:59

标签: matlab

我有一个多功能脚本,应该向用户询问4辆不同的汽车,并根据额定值对它们进行加权,从而为用户提供最佳的购买汽车。 我要做的是提示用户输入的每辆汽车,以便用户可以输入用户决定使用的每个变量的数据。但是,在给提示添加标题时,我想在提示中使用汽车名称。对我来说似乎是不可能的,我不确定该怎么做,这对编码来说是很新的。


主脚本

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

Group_Function1

function [N] = Group_Function1(Q1)
for Q = 1:Q1
prompt = {'Name of Car:'};
title = 'Car Name';          
answer = inputdlg(prompt,title, [1 80])'; 
N(Q) = answer(1);
end

Group_Function2

function [V,W] = Group_Function2(Q2)

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

Group_Function3(出现问题的地方)

function [H] = Group_Function3(V)
prompt = {V};
title = ['Variable Ratings For' Group_Function1(answer{1})];
h = inputdlg(prompt, title, [1 80])';
end

问题

对于“ Group_Function3”,我希望提示包括来自“ Group_Function1”的用户输入,以便在提示输入答案时,我知道我要输入哪辆车。

1 个答案:

答案 0 :(得分:1)

每个function在自己的工作空间中运行,这意味着它不知道其外部变量的状态或内容。如果要让某个功能知道某些特定信息(例如汽车名称),则必须在输入参数中将其赋予该功能。一个函数可以有多个输入参数,而不仅限于一个。

在进入Group_Function3之前,我想为Group_Function1提出一种新方法。


Group_Function1:

您运行一个循环以独立询问每个汽车名称。必须验证每个对话框非常繁琐。这是一种一次性查询4个汽车名称的方法:

将脚本的开头替换为:

title1  = 'Cars';
prompt1 = {'How Many Cars (4): '};
answer1 = inputdlg(prompt1, title1 );

nCars   = str2double( answer1{1} );

CarNames = getCarNames(nCars) ; % <= use this function
% [N] = Group_Function1(Q1);    % instead of this one

并将Group_Function1替换为:

function CarNames = getCarNames(nCars)
    title = 'Car Names';          
    prompt = cellstr( [repmat('Name of car #',nCars,1) , sprintf('%d',(1:nCars)).'] ) ;
    CarNames = inputdlg( prompt, title, [1 80] ) ; 
end

现在CarNames是一个单元格数组,其中包含您4辆汽车的名称(就像变量N之前所做的那样。我建议使用更明确的变量名)。

您可以按原样运行其余代码(只需将N替换为CarNames,将Q1替换为nCars)。


Group_Function3:

当您进入Group_Function3时,必须将当前的汽车名称发送给函数(以便它可以在标题或提示中使用该名称)。因此,将您的Group_Function3替换为以下内容(我们将输入变量添加到函数定义中):

function H = Group_Function3( V , thisCarName )
    prompt = {V};
    title = ['Variable Ratings For' thisCarName];
    H = inputdlg(prompt, title, [1 80])';
end

并在您的主脚本中这样调用:

for h = 1:nCars
    thisCarName = carNames{h} ;
    H = Group_Function3( V , thisCarName ) ;
    % ...
    % anything else you want to do in this loop
end
相关问题