Error: Index in position 2 exceeds array bounds
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Zuha Yousuf
el 14 de Ag. de 2019
Comentada: Neuropragmatist
el 14 de Ag. de 2019
I'm trying to import an xlsx file into MATLAB and make a simple graph of heart rate vs time. The time vector is from row 386 till 1645, and column 6 and the heart rate vector is from row 386 till 1645 and column 7. I get an error at the line HR = cellfun(@str2double, NumDataS(:,2)); where it says Index in position 2 exceeds array bounds. I have attached the .xlsx file here. Can anyone please help me in understanding where this error is coming from and how to fix it?
MATLAB CODE:
[~,NumDataS]=xlsread('Pluto_5.24.19_Dex_vitals_001.xlsx','Sheet2','F386:G1645'); % Physiological Data
[~,TmatrixS] = xlsread('Pluto_5.24.19_Dex_vitals_001.xlsx','Sheet2','A386:F1645'); % Time Data
HR = cellfun(@str2double, NumDataS(:,2)); % Retrieve From Cell Array Of Strings
Time1=cellfun(@str2double, TmatrixS(:,5));%Array of minutes to be converted to seconds
0 comentarios
Respuesta aceptada
Star Strider
el 14 de Ag. de 2019
I recognised my code, so I went back and looked this up. (Reference: Error: Index in position 1 exceeds array bounds.)
Your Excel file does not encode everything in string variables this time, so a much more conventional approach will work.. Also, ‘NumData’ (actually ‘HR’) is a single column, not two, so your xlsread call for it must reflect that.
Try this:
[NumData]=xlsread('Pluto_5.24.19_Dex_vitals_001.xlsx','Sheet2','G386:G1645'); % Physiological Data
[Tmatrix] = xlsread('Pluto_5.24.19_Dex_vitals_001.xlsx','Sheet2','A386:F1645'); % Time Data
HR = NumData;
Time = datenum(Tmatrix); % Date Numbers
DTime = datetime(Tmatrix); % ‘datetime’ Array
figure
graph1=plot(Time, HR);
datetick('x', 'HH:MM:SS.FFF', 'keepticks')
figure
graph2=plot(DTime, HR);
This worked when I ran it.
2 comentarios
Más respuestas (1)
Neuropragmatist
el 14 de Ag. de 2019
Editada: Neuropragmatist
el 14 de Ag. de 2019
Your line fails because NumDataS is empty, because the 'text' field of xlsread is empty. I'm not really sure why but the third output of xlsread seems to contain the cell array you are expecting.
So your code will work if you just change to this line:
[~,~,NumDataS]=xlsread('Pluto_5.24.19_Dex_vitals_001.xlsx','Sheet2','F386:G1645');
I'm guessing you will have to do the same for the other spreadsheet, but you will have to check:
[~,~,TmatrixS] = xlsread('Pluto_5.24.19_Dex_vitals_001.xlsx','Sheet2','A386:F1645');
Hope this helps,
M.
4 comentarios
Star Strider
el 14 de Ag. de 2019
@Metioche — Your code is essentially correct (adapting code that I wrote earlier), and has a context of an earlier Question I Answered. The problem is that:
[~,~,NumDataS]=xlsread('Pluto_5.24.19_Dex_vitals_001.xlsx','Sheet2','F386:G1645');
↑
should be:
[~,~,NumDataS]=xlsread('Pluto_5.24.19_Dex_vitals_001.xlsx','Sheet2','G386:G1645');
to read only the ‘HR’ column, not the ‘seconds’ column from the time array as well. The extra column is an error that causes problems with the rest of the code.
Neuropragmatist
el 14 de Ag. de 2019
Ah I see now, I didn't check the HR = line, this could easily be fixed in my code by using:
HR = cell2mat(NumDataS(:,2));
Good that you caught it though,
M.
Ver también
Categorías
Más información sobre Data Import from MATLAB en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!