Search code examples
matlabloopsfor-loopxlsread

Reading in xlsm file into MATLAB | Error file name must be string


I am trying to read in a whole set of environmental input/output data from the WIOD (World Input output database) through a nested loop over the countries and years. Now I have done something similar for the base/data accounts before. Now I try to load in the environmental data. My previously working code looked as follows:

 for yr = 95:99
   V(:,:,yr-94) = xlsread(['wiot' num2str(yr)   '_row_apr12.xlsx'],['WIOT_19' 
   num2str(yr)],'E1443:BCI1448');
 end

Now my code which is not working with the error message "File name must be a character vector." looks as follows:

 country = 
{'AUS','AUT','BEL','BGR','BRA','CAN','CHN','CYP','CZE','DEU','DNK','ESP',...

'EST','FIN','FRA','GBR','GRC','HUN','IDN','IND','IRL','ITA','JPN','KOR',...

 'LTU','LUX','LVA','MEX','MLT','NLD','POL','PRT','ROU','RUS','SVK','SVN',...
       'SWE','TUR','TWN','USA','ROW'}

for c = 1:41

   for year = 1995:1995   

      F_NRG(:,(c*35)-34:(c*35),year-1994) = transpose(xlsread([country(c) 
     '_EU_May12.xlsm'],[num2str(year)],'AD2:AD36'));

   end
end

I do not get it because the filename should be a string if I select the country c via country(c)? The xlsread is nested in a transpose command and the cells where I want to save the read in data are computed slightly more complicated but principally it should be the same? The following code also does render a string for each c.

  for c = 1:41
    country(c)
  end 

Can you help me find my coding mistakes? Why doesn't Matlab recognize the file name as a string?

Thank you for your help.


Solution

  • Try this,

    for c = 1:41
    
       for year = 1995:1995   
    
          F_NRG(:,(c*35)-34:(c*35),year-1994) = transpose(xlsread([country{c} 
         '_EU_May12.xlsm'],[num2str(year)],'AD2:AD36'));
    
       end
    end