MATLAB Answers

0

Error using xlsread (line 132) File name must be a character vector. Error in Lab1 (line 8) trial_data1= xlsread(Raw_Data(i),1);

Muhimbo Karangizi さんによって質問されました 2019 年 1 月 20 日
最新アクティビティ Stephan
さんによって コメントされました 2019 年 1 月 21 日
I've been writing the code to plot the data for a Lab and I'm using a data structure method but I keep getting an error (in my four loop where I use xlsread to read all the files). The error says that I need Raw_Data(which contains an array of all the files I'm calling) as a character vector, in order for xlsread to work. I'm not sure how to do this and I have tried using Char and other methods but it still won't work. Attached is the code. 'CH0' and 'Ch1' are columns in my excel sheet that contain the data I need.
Raw_Data = {'Hot Water Trial 1.xlsx','Hot Water Trial 2.xlsx','Hot Water Trial 3.xlsx','Room Temp Trial 1.xlsx','Room Temp Trial 2.xlsx','Room Temp Trial 3.xlsx','Ice Water Trial 1.xlsx','Ice Water Trial 2.xlsx','Ice Water Trial 3.xlsx'};
data = struct('CH0',[],'CH1',[]);
for i = 1:length(Raw_Data)
trial_data1= xlsread(Raw_Data(i),1);
data(i).CH0=trial_data1(:,3);
data(i).CH1=trial_data1(:,4);
data(i).CH0_mean=mean(data(i).CH0);
data(i).CH1_mean=mean(data(i).CH1);
end

  0 件のコメント

サインイン to comment.

1 件の回答

回答者: Stephan
2019 年 1 月 21 日
 採用された回答

Hi,
since Raw_data is a cell array use {i} to get the content at i in your for loop:
trial_data1= xlsread(Raw_Data{i},1);
instead of:
trial_data1= xlsread(Raw_Data(i),1);
You can see the difference here:
>> a = Raw_Data{1}
a =
'Hot Water Trial 1.xlsx'
>> whos a
Name Size Bytes Class Attributes
a 1x22 44 char
>> b = Raw_Data(1)
is different to:
b =
1×1 cell array
{'Hot Water Trial 1.xlsx'}
>> whos b
Name Size Bytes Class Attributes
b 1x1 156 cell
Best regards
Stephan

  2 件のコメント

Thank you Stephan !
This fixed my problem and will help me in the future with understanding Structs.
Stephan
2019 年 1 月 21 日
if my answer was useful please accept it.

サインイン to comment.



Translated by