Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matlab function return value

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!

like image 508
CSPTT Avatar asked Apr 11 '13 17:04

CSPTT


People also ask

Is there a return function in MATLAB?

return forces MATLAB® to return control to the invoking program before it reaches the end of the script or function. The invoking program is a script or function that calls the script or function containing the call to return .

Can MATLAB function return multiple values?

Matlab allows you to return multiple values as well as receive them inline. Save this answer. Show activity on this post.

What does %% mean in MATLAB?

The MATLAB editor automatically highlights all content after %% comments, and text after %% in the same line are turned bold.


1 Answers

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.

like image 125
yuk Avatar answered Nov 03 '22 01:11

yuk