creating error_msg function

The issue I have is that:
You must write 4 functions. Three functions are for the area calculations: calc_circle_area, calc_rect_area, calc_triangle_are. These functions should do a calculation but NO input from the user. All user interaction must be done in the main script. The fourth function is called error_msg. Given an input string, it should display an appropriate message. For example
>> error_msg(base)
value for base must be > 0
no calculcation will be performed
>> error_msg('length')
value for length must be > 0
no calculcation will be performed
Hint: to concatenate two strings, enclose them in [ ]. For example,
>> disp( ['Hello' 'world'])
Helloworld
I have made the first three functions, I just am having problem creating the error_msg function. I don't understand how to do it, if anyone could help that would be great!

답변 (1개)

Walter Roberson
Walter Roberson 2011년 10월 4일

0 개 추천

function error_msg(Name)
phrase1 = 'value for ';
phrase2 = ' must be > 0\nno calculation will be performed\n';
phrase = char(zeros(1,length(phrase1)+length(Name)+length(phrase2));
for K = 1 : length(phrase1)
phrase(K) = phrase1(K);
end
for K = 1 : length(Name)
phrase(length(phrase1)+K) = Name(K);
end
for K = 1 : length(phrase2)
phrase(length(phrase1)+length(phrase2)+K) = phrase2(K);
end
fprintf(any(phrase),phrase);
end

댓글 수: 7

Fangjun Jiang
Fangjun Jiang 2011년 10월 4일
What is your point, Walter?
Walter Roberson
Walter Roberson 2011년 10월 4일
Sorry, fixed the bug.
Fangjun Jiang
Fangjun Jiang 2011년 10월 4일
Walter, you probably don't have MATLAB to test it out. But I thought it could be done by:
fprintf('value for %s must be >0\nno calculation will be performed\n',Name);
Fangjun Jiang
Fangjun Jiang 2011년 10월 4일
Or use the hint that was suggested in the assignment.
disp(['value for ',Name,' must be >0']);
disp('no calculation will be performed');
Walter Roberson
Walter Roberson 2011년 10월 5일
The code form I used is very close to what would have to be used in a microprocessor in which string constants are compiled in to read-only memory.
shanon
shanon 2011년 10월 5일
Thank you both very much! The programming I am doing is very simple, so I used Fangjun's answer, but they both are great. Thanks for your time!
Fangjun Jiang
Fangjun Jiang 2011년 10월 5일
Walter, keep working on it. As of now, there are errors in the code. Passing that, fprintf() has run-time error.

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

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

질문:

2011년 10월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by