5

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

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".

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

exemple:

John doughlas 15986

Filip duch 357852

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?

Grateful for all the answers!

2 Answers 2

13

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

For example, in

function y = student(j)                                

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

Read more about functions in MATLAB.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you, I figure out where to put it but cant figure out the text.m, it has to many output.
You can return multiple outputs from a function with [x,y,z] = functionname(...). Or you can combine all outputs into a cell array or a structure. Read the documentation, it's clearly explained. And don't call the function text, since there is a build-in MATLAB function with this name.
Sorry but I cant find the result I am looking for maybe I am just to close to see it or I am blind. But I read the documentation and I got some results but not what I am looking for. Like exemple: y=25 y= numbers name1,2,3,4,5 and so on y= numbers name1 y = numbers name2 and so on.
Sorry, but I cannot understand what you need. Please explain in details what result do you expect. Better in a new question, since you've already accepted this one. You can refer to this question with a link.
2

Here is a working example.

The first part is to create a function called 'functionA' in a filename 'functionA.m'. Then put the following code inside:

    function  result = functionA(N,alpha)
    result = 5;
    return
    end

The second part is to create another Matlab file(i.e. upto you to name it) or you can use the Matlab command window even. Then run the following code:

    getresult = functionA(100,10);
    getresult

After running you get the following answer:

    ans = 
          5

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.