output to console from within function

27 ビュー (過去 30 日間)
Nicholas  Jacobs
Nicholas Jacobs 2016 年 2 月 21 日
編集済み: Stephen23 2016 年 2 月 21 日
I'm trying to create a function that takes two vectors. I use a for loop with nested if/else to process one vector(want) and depending on the char values within it will display a statement containing values in ret. I'm basiclly displaying results of calculations to the user.
When I attempt to do this in the function presentAns, I can at most only get one statement to display on the command line despite there being multiple values in want. I assume after it displays it falls out of the function.
What I want is matlab to display all three results in succession without leaving the function presentAns.
The code has mistakes in it, I know, I'm including it to help convey what I"m trying to do. The best luck I had is using fprintf but that would only display 1 statement and exit back to main, despite there being other cases in 'want'.
function [ out ] = presentRes( want,ret )
%presentRes is used to select which output statements are selected to
%present the user with the results of their desired equations.
len = length(want);
out = ones(len,1);
for k = 1:len
% x = ['Your power factor is ' num2str(pe), ' ,it is', name];
if want(k) == 'S'
fprintf(' Your complex power calculation is', num2str(ret(k)),'.',);
elseif want(k) == 'p'
x1 = [' Your real power value is', num2str(ret(k)),'.'];
elseif 'I'
x2 = [' Your phase current value is',num2str(ret(k)),'.'];
elseif 'Q'
x3 = [' Your reactive power value is', num2str(ret(k)),'.'];
elseif 's' %apparent power
x4 = [' Your apparent power value is' ,num2str(ret(k)),'.'];
elseif 't'
x5 = [' Your power factor value is' ,num2str(ret(k)),'.'];
elseif 'z'
x6 = [' Your load impedence value is' ,num2str(ret(k)),'.'];
end
end
end
end

回答 (1 件)

Jan
Jan 2016 年 2 月 21 日
編集済み: Stephen23 2016 年 2 月 21 日
elseif 'I' is not a comparison, but the expression 'I' is converted to a logical, which means true, because the CHAR value of 'I' is not zero.
Your function is not called "presentAns", in opposite to your description.
The output out is not modified. The meaning of "x1", "x2", ... is unclear.
Guessing like "it falls out of the function" is not useful for programming. Better use the debugger to step through your code line by line. Search in the documentation for instructions about debugging.
Perhaps you want something like this:
function out = presentRes(want, ret)
len = length(want);
out = cell(len, 1);
Msg = num2str(ret(k));
for k = 1:len
switch want(k)
case 'S'
out{k} = [' Your complex power calculation is', Msg,'.'];
case 'p'
out{k} = [' Your real power value is', Msg,'.'];
case 'I'
out{k} = [' Your phase current value is', Msg, '.'];
otherwise
error('Character not handled: %s', want(k));
end
end
fprintf('%s\n', out{:});
But this is guessed boldly. Better explain, what you want to achieve.

カテゴリ

Help Center および File ExchangeWhos についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by