How can i do let Matlab returns variable name when i check its value?

조회 수: 8 (최근 30일)
Assumed that, i have a code as below:
x=input('x = ');
y=input('y= ');
z=input(z= ');
...and so on.
And my condition is value of any x , y or z... (that user will give into accrossding to keyboard) is must be positive. And i mean is after user give into value of variables above. How can i do to know what variable is negative. For example, I want to my program warning that: " Variable x is negative", My code that i tried. I create a vecto that called "Ktra" and use a for loop.
x=input('x= ');
y=input('y= ');
z=input('z= ');
Ktra=[x y z];
iam = 1;
for i=1:length(Ktra)
if Ktra(i)<0
disp('Gia tri nhap can lon hon 0');
tprintf('So thu tu cua so bi am la: %d \n', iam);
end
end
Thank you so much!

채택된 답변

Stephen23
Stephen23 2017년 2월 4일
편집: Stephen23 2017년 2월 4일
This works for any number of variables, just add them to the cell array V:
V = {'x','y','z'};
S = struct();
for k = 1:numel(V)
str = sprintf('Please enter %s value: ',V{k});
tmp = str2double(input(str,'s'));
if tmp<0
disp('Gia tri nhap can lon hon 0');
fprintf('So thu tu cua so bi am la: %s \n',V{k})
end
S.(V{k}) = tmp;
end
When run it gives this:
>>
Please enter x value: 23
Please enter y value: 5
Please enter z value: -9
Gia tri nhap can lon hon 0
So thu tu cua so bi am la: z
>>
and of course the values can be simply accessed using the structure fields:
>> S.x
ans =
23
>> S.z
ans =
-9
Note also that I used the faster and more secure option of getting input to return a string: str2double(input(...,'s')), as opposed to the buggy and unpredictable input(...).
  댓글 수: 2
Le Dung
Le Dung 2017년 2월 9일
편집: Walter Roberson 2017년 2월 9일

Thank you so much. it's usefull to me.

But, now. because, here, we have a loop. Assumed that, (for example: x and y).If we have two or more values are negative. So, When program runs, it gives this:

Gia tri nhap can lon hon 0
So thu tu cua so bi am la: x
Gia tri nhap can lon hon 0
So thu tu cua so bi am la: y

I mean, program provides for us two warnings: "Gia tri nhap can lon hon 0".

Of cause, it isn't favourable. So if i only want to one waring for any case. How can i do?

Best Regard!

Walter Roberson
Walter Roberson 2017년 2월 9일
V = {'x','y','z'};
S = struct();
gave_warning = false;
for k = 1:numel(V)
str = sprintf('Please enter %s value: ',V{k});
tmp = str2double(input(str,'s'));
if ~gave_warning && any(tmp<0)
disp('Gia tri nhap can lon hon 0');
fprintf('So thu tu cua so bi am la: %s \n',V{k});
gave_warning = true;
end
S.(V{k}) = tmp;
end

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2017년 2월 4일
Varnames = {'x', 'y', 'z'}
Now you can display Varnames{i}
  댓글 수: 1
Le Dung
Le Dung 2017년 2월 4일
First thank you so much. But, Could you explain more detail? because i'm begineer. thank you so much.

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

카테고리

Help CenterFile Exchange에서 Numerical Integration and Differential Equations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by