0

EDIT: I figured out the problem. claim was listed as a cell class. Used cell2mat to convert it to char, and the code worked. Thank you, everybody!

I have a string variable set to a file pathway/location. I would like to use this variable as the input to the xlsread function, but Matlab tells me that xlsread cannot take a variable input. I'm having lots of trouble figuring whether a workaround is even possible. Can someone help me out?

 function C = claimReader()

 inp = csv2struct(['C:\Documents and Settings\nkulczy\My Documents\085 Starry Sky\','Starry_Sky_inputs_vert.csv']);
 inputTitles = [{'Output_Dir'};{'Claimz'};{'Prodz'};{'Fault_Locations'};{'Fault_Type'};{'Primary_Failed_Part'};{'Part_Group'};{'Selected_SEAG'};{'Change_Point'};{'Date_Compile'};{'Minimum_Date'}];

 claim = inp.(cell2mat(inputTitles(2)));  %returns a file path/location string

 C = csv2struct(claim);

 end


 function Out = csv2struct(filename)

 %% read xls file with a single header row

 [~, ~, raw] = xlsread(filename);

 [~ , ~, ext] = fileparts(filename);

 if ~strcmpi(ext, '.csv') %convert non .csv files to .csv, so blanks stay blank
      filename=[pwd,'tempcsv',datestr(now,'yymmddHHMMSSFFF'),'.csv'];
      xlswrite(filename,raw);
      [~ , ~, raw] = xlsread(filename);
      delete(filename);
 end

 if size(raw,1)==11 && size(raw,2)==2 %transpose SS inputs (must      match dimensions of input matrix EXACTLY!!!)
     raw = raw';
 end

 nRow = size(raw,1);
 nCol = size(raw,2);
 header = raw(1,:);
 raw(1,:) = [];

 end
  • 2
    You're doing something wrong. Post your code. – Peter Oct 02 '13 at 14:06
  • It's definitely possible, look a the examples in the docs: http://www.mathworks.com/help/matlab/ref/xlsread.html. Post code because you've most likely just made a small error somewhere – Dan Oct 02 '13 at 14:09
  • I made some edits, does that help? – Robert Seifert Oct 02 '13 at 14:55
  • Yeah, I figured out the problem. I was inadvertently referencing the 1x1 cell, rather than the string inside it. I'm an extreme amateur when it comes to matlab, and the learning curve is rather steep. – user2447846 Oct 02 '13 at 15:16

2 Answers2

3

Use the syntax as following and there shouldn't be any problems:

pathname = 'c:\...\filename.xlsx';
A = xlsread(pathname);

Edit: regarding your code:

I can't see where you define filename - you should pass claim (it contains the desired path?) to the xlsread-function. Probably you get a cell with chars. So your input needs to be claim{1}

Robert Seifert
  • 25,078
  • 11
  • 68
  • 113
1

Check the documentation of xlsread, the first output (yourNums) will only return the numerical values in the sheet. txt will only return text. rawData will return the raw data in the sheet.

flNm = 'c:\myFolder\myFile.xlsx';

[yourNums, txt, rawData] = xlsread(flNm);

Update after TS:

claim is a cell array. So you need to pass claim{1} in order to let it be a string.

Nick
  • 3,143
  • 20
  • 34
  • If I manually insert the filepath and location, [~,~,raw] gives the the raw data just fine. My error is: `??? Error using ==> xlsread at 122` `Filename must be a string.` – user2447846 Oct 02 '13 at 14:47