简体   繁体   中英

Matlab too many outputs

The program myfile.m reads a txt file that contains a total of 25 names and numbers like

exemple:

John doughlas 15986

Filip duch 357852

and so on.

The program converts them to

15986 Doughlas John
357852 duch Filip 

This is without function, with it I get too many outputs.

Error message:
Error using disp
Too many output arguments.

Error in red4 (line 26)
  array = disp(All);

Original code below:

function array = myfile(~) 
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);

Now I have got many good answers from people and sites like functions but I cant get the results like the above with function.

I have tried combining them and got some results:

y = 15986 & [a,z,b] = myfile
y = 25 & myfile = x
y = numbers name1,2,3,4 and so one & myfile = fprintf(All)
y =                       & I used results().namn, 
numbers name 1            & results().id, results().lastname
y =
numbers name 2 and so on.

The result I want is:

y = myfile
y =
15986 Doughlas John
357852 duch Filip 

update: Change it like Eitan T said but did't get the result like above.

Got the result:

'John doughlas 15986'

'Filip duch 357852'

function C = myfile()                            
 if nargin == 0 
    dirr = '.';
end
answer = dir(dirr); 
k=1;
while k <= length(answer)   
    if answer(k).isdir 
        answer(k)=[]; 
    else
        filname{k}=answer(k).name;
        k=k+1;
    end
end
chose=menu( 'choose',filname); 
name = char(filname(chose)); 
fid = fopen(name, 'r');
C = textscan(fid, '%s', 'delimiter', '');
C = regexprep(C{1}, '(\w+) (\w+) (\w+)', '$3 $2 $1');
fclose(fid); 

Why use loops? Read the lines at once with textscan and use regexprep to manipulate the words:

fid = fopen(filename, 'r');
C = textscan(fid, '%s', 'delimiter', '');
C = regexprep(C{1}, '(\w+) (\w+) (\w+)', '$3 $2 $1')
fclose(fid);

The result is a cell array C , each cell storing a line. For your example, you'll get a 2×1 cell array:

C = 
    '15986 doughlas John'
    '357852 duch Filip'

I'm not sure what you want to do with it, but if you provide more details I can improve my answer further.

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM