convert number to string

function Sex= Choose_Sex
disp('Choose Sex or Gender to preview Health Data on')
Sex = menu('Choose sex','Female','Male');
if Sex == 1
disp('Female')
elseif Sex == 2
disp('Male')
end
while Sex == 0
disp('Error Choose sex,');
Sex = menu('Choose sex','Female','Male');
end
How can i let the answer of Sex show as string female or male instead of double 1 or 2

回答 (3 件)

Walter Roberson
Walter Roberson 2021 年 12 月 12 日

0 投票

while true
choice = menu(etc)
if choice == 1
Sex = "Female";
break;
elseif choice == 2
Sex = "Male";
break
end
fprintf('What??\n');
end

2 件のコメント

Khaled Awda
Khaled Awda 2021 年 12 月 12 日
still show sex as a box in a fprintf statement
Walter Roberson
Walter Roberson 2021 年 12 月 12 日
No it does not. The only fprintf() statement there is for the case that neither male nor female is chosen, and in that case it displays a hard-coded string with no box.
You might perhaps have a different fprintf() statement that you have not shown us.

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

Voss
Voss 2021 年 12 月 12 日
編集済み: Voss 2021 年 12 月 12 日

0 投票

You can use a cell array of character vectors (formerly known as a cell array of strings) to store the options.
function Sex= Choose_Sex
disp('Choose Sex or Gender to preview Health Data on')
Sexes = {'Female' 'Male'}; % 'Sexes' is a cell array of character vectors
Sex = menu('Choose sex',Sexes{:});
if Sex > 0
disp(Sexes{Sex});
end
while Sex == 0
disp('Error Choose sex,');
Sex = menu('Choose sex',Sexes{:});
% Edit: I guess you want to display the sex here too
if Sex > 0
disp(Sexes{Sex});
end
end

2 件のコメント

Khaled Awda
Khaled Awda 2021 年 12 月 12 日
still shows as number in fprintf statement
Voss
Voss 2021 年 12 月 12 日
Return the character vector from the Choose_Sex function rather than the index Sex:
function Sex= Choose_Sex
disp('Choose Sex or Gender to preview Health Data on')
Sexes = {'Female' 'Male'}; % 'Sexes' is a cell array of character vectors
Sex = menu('Choose sex',Sexes{:});
if Sex > 0
disp(Sexes{Sex});
end
while Sex == 0
disp('Error Choose sex,');
Sex = menu('Choose sex',Sexes{:});
% Edit: I guess you want to display the sex here too
if Sex > 0
disp(Sexes{Sex});
end
end
Sex = Sexes{Sex}; % return the character vector, not the index
Then, when calling the function and printing the result, use the %s format specifier for strings/character vectors:
selected_sex = Choose_Sex();
fprintf('Selected Sex is %s',selected_sex);

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

Image Analyst
Image Analyst 2021 年 12 月 12 日

0 投票

Try this:
function Sex= Choose_Sex
disp('Choose Sex or Gender to preview Health Data on')
selectedButton = menu('Choose sex','Female','Male');
Sex = 'Male'; % Initialize.
if selectedButton == 1
disp('Female')
Sex = 'Female';
elseif Sex == 2
disp('Male')
end

4 件のコメント

Khaled Awda
Khaled Awda 2021 年 12 月 12 日
still shows as number in fprintf statement
Image Analyst
Image Analyst 2021 年 12 月 12 日
編集済み: Image Analyst 2021 年 12 月 12 日
I don't know how you're using fprintf(). Please note that my code works fine, does not display any numbers, and does not use fprintf().
Full and complete demo if you need it is below. Put this all into one m-file exactly as is:
% Demo by Image Analyst
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
markerSize = 40;
Sex= Choose_Sex
function Sex= Choose_Sex
disp('Choose Sex or Gender to preview Health Data on')
selectedButton = menu('Choose sex','Female','Male');
Sex = 'Male'; % Initialize.
if selectedButton == 1
disp('Female')
Sex = 'Female';
elseif Sex == 2
disp('Male')
end
end
Khaled Awda
Khaled Awda 2021 年 12 月 12 日
doesnt run
Image Analyst
Image Analyst 2021 年 12 月 12 日
It DOES run. I just tried it again. It's now attached. If it doesn't run for you, then attach your code.

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

タグ

質問済み:

2021 年 12 月 12 日

コメント済み:

2021 年 12 月 12 日

Community Treasure Hunt

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

Start Hunting!

Translated by