本文介绍了Matlab函数返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序,该程序具有功能和问题,返回值,它的输出太多.

I have one program that has function and the problem, return value, it has too many output.

例如exempley:y =出现答案的文字

Like exempley: y = text the answer comes up

Error in text (line 2)

if nargin == 0

Output argument "array" (and maybe others) not assigned during call to "
C:\Users\name\Documents\MATLAB\text.m>text".

程序text.m读取一个txt文件,其中包含几个名称和数字,例如

The program text.m reads a txt file that contains a couple of names and numbers like

例如:

约翰·唐格拉斯15986

John doughlas 15986

Filip duch 357852

Filip duch 357852

,依此类推.该程序将它们转换为15986 Doughlas John等.

and so on. The program convert them to 15986 Doughlas John and so on.

function array = text(~)
if nargin == 0
dirr = '.';
end
answer = dir(dirr);
k=1;
while k <= length(answer)
    if answer(k).isdir
        answer(k)=[];
    else
        filename{k}=answer(k).name;
        k=k+1;
    end
 end
chose=menu( 'choose file',filename);
namn = char(filename(chose));
fid = fopen(namn, 'r');
R = textscan(fid,'%s %s %s');
x=-1;
k=0;
while x <= 24
      x = k + 1;
      All = [R{3}{x},'   ',R{1}{x},' ',R{2}{x}];
      disp(All)
      k = k + 1;
end
fclose(fid);

反正有没有从头开始解决问题的方法?

Is there anyway to fix the problem without starting over from scratch?

感谢所有答案!

推荐答案

您在定义中指定了函数输出参数,但没有在函数主体中为其分配任何内容.

You specify the function output argument in the definition, but you don't assign anything to it in the function body.

例如,在

function y = student(j)

您的输出是y.因此,您必须为y分配一些内容.

your output is y. So you have to assign something to y.

了解有关MATLAB中函数的更多信息.

Read more about functions in MATLAB.

这篇关于Matlab函数返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 05:26