【问题标题】:How to give value of string to another string in matlab如何在matlab中将字符串的值赋予另一个字符串
【发布时间】:2015-02-27 20:21:36
【问题描述】:

我有一个简单的问题。我想计算训练心率。我有一些值。 RHR 表示静息心率。 INTEN 表示健身水平,我为低、中和高健身水平给出值 0.55,0.65,0.8 我编写了该代码

 Gender=input('Please input your gender: ');
 Age=input('Please input your age: ');
 RHR=input('Please enter your resting heart rate: ');
 INTEN=input('Please enter your fitness level(low,medium or high): ');
 male=Male;
 female=Female;
 low=0.55;
 medium=0.65;
 high=0.8;
 if INTEN==0.55
 INTENT=0.55;
 elseif INTENT==medium
INTENT=0.65;
else 
INTENT=0.8;
end
 if Gender==Male
 THR=((220-Age)-RHR)*INTEN+RHR;
 elseif Gender==Female
 THR=((206-0.88*Age)-RHR)*INTEN+RHR;
 end
  disp('The recommended training heart rate is ',num2str(THR))

但为什么会报错?

【问题讨论】:

  • 正确缩进代码,显示测试用例,显示错误。
  • 什么是MaleFemale
  • 我想给另一个变量工作
  • input(blablabla,'s')指定字符串输入
  • @TryHard - 从我嘴里说出来的话!

标签: string matlab math if-statement


【解决方案1】:

您的代码中有几个错误。值得注意的是,您正在使用保留的操作来将数字与字符串进行比较,这是不可能的。此外,变量Gender 应该是一个字符串,但被操作为一个数字,这是令人困惑的。请务必查看用于比较字符串的函数strcmp。然后您可以使用if/elseif 块。

我建议使用prompt 来查询用户的信息。这样一来,所有内容都会同时显示,并且在我看来更易于使用。

这是带有 cmets 的代码。如果有什么不清楚的地方请告诉我。

clc
clear

%// Set up dialog promt.
prompt = {'Enter your gender (male/female)','Enter your age:','Enter your resting heart rate: ','Enter your fitness level(low,medium or high): '};
dlg_title = 'Input';
num_lines = 1;

%// Default answers
def = {'male','30','120','medium'};

%// The answers are stored in the cell array called "answer". Its a 4x1
%// cell array containing ONLY STRINGS.
answer = inputdlg(prompt,dlg_title,num_lines,def);

%// Transform the strings into numbers that you can use.
Gender = answer{1};
Age = str2double(answer{2});
RHR = str2double(answer{3});
INTEN = answer{4};

%// A switch/case statement to convert INTEN into the number used for
%// the calculation
switch INTEN
    case 'low'
        INTEN=0.55;
    case 'medium'
         INTEN=0.65;
    case 'high'
        INTEN=0.8;
end

%// Use strcmp to compare strings. 
if strcmp(Gender,'Male') || strcmp(Gender,'male')

    THR=(220-Age-RHR)*INTEN+RHR;

elseif strcmp(Gender,'Female') || strcmp(Gender,'female')

    THR=((06-0.88*Age)-RHR)*INTEN+RHR;

end

%// Create a string to display
DispMessage = sprintf('The recommended training heart rate is %0.2f\n',THR);

%// Create a message box to display the above string.
msgbox(DispMessage)

提示窗口如下所示:

以及显示的消息:

希望有帮助!

【讨论】:

  • 这远远超出了要求。 +1。
  • 我的荣幸!欢迎来到 Matlab 和字符串的世界哈哈玩得开心:)
【解决方案2】:

Matlab 无法确定您输入的内容。正确的版本是:

 Gender=input('Please input your gender: ','s');

此外,您的代码中还有更多错误,我建议您自己修复。我的不太先进的版本然后上:

Gender=input('Please input your gender: ','s');
Age=input('Please input your age: ');
RHR=input('Please enter your resting heart rate: ');
INTEN=input('Please enter your fitness level(low,medium or high): ','s');
if strcmp(INTEN,'low')
INTENT=0.55;
elseif strcmp(INTEN,'medium')
INTEN=0.65;
else 
INTEN=0.8;
end

if strcmp(Gender,'male')|| strcmp(Gender,'Male')
THR=((220-Age)-RHR)*INTEN+RHR;
if strcmp(Gender,'female')|| strcmp(Gender,'Female')
THR=((206-0.88*Age)-RHR)*INTEN+RHR;
end

printres=['The recommended training heart rate is ',num2str(THR)];
disp(printres)

【讨论】:

  • THR的结果不正确
  • 已修复,已在男性案例上调试过
猜你喜欢
  • 2023-02-16
  • 1970-01-01
  • 1970-01-01
  • 2012-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-14
相关资源
最近更新 更多