Search code examples
matlabfor-loopxlsreaddisp

Using disp function to display values


Data.xlsx is an excel file containing the data for 156 students. From cell 4 of the excel file, the 1st Student Number is displayed and if you increment by 7, you have the 2nd Student Number and so on up until the last cell 1094.

F = xlsread('Data.xlsx');

for ii =  4:7:1094
    studentNumbers = disp(F(ii)); 
end

but this gives me an error saying "Too many output arguments". However if I just specify disp(F(ii)), it does not give me any error and displays all the student numbers in order.

Is there a better way to do this so that I can assign all these Student Numbers into an array called studentNumbers?


Solution

  • disp is a function that displays the number on screen, not assigns it to another variable. If you want to assign you just use =. So your loop should be:

    for ii =  4:7:1094
        studentNumbers = F(ii); 
    end
    

    Now, as @Sardar has pointed correctly, there is no need for a loop here, you can just assign all values in one command, using the vector 4:7:1094 as an indexing. As in his answer:

    studentNumbers = F(4:7:1094);
    

    But, if you can define the range of the data in Excel, then xlread lets you skip this assignment by setting the function to read a specific range, like:

    sheet = 1; % if the data in in sheet 1
    xlRange = 'D:D'; % if all the data is in column D
    studentNumbers = xlsread('Data.xlsx',sheet,xlRange)