Index exceeds the number of array elements (0).
Show older comments
Hi,
I'm struggling with a piece of code to extract information from a text file. It keeps coming up with the error message 'Index exceeds the number of array elements (0).'. I think this is something to do with the format of my data but I'm not super experienced when it comes to extracting data.
My code:
% Enter BETR text file name and location
fid = fopen('filename);
% read the file and split it into lines
data = textscan(fid, '%s', 'delimiter', '\n');
data = data{1}; % extract cell array from textscan output
% close the file
fclose(fid);
% Initialize empty arrays for hours, M1, M2, and M3
hours = [];
m1 = [];
m2 = [];
m3 = [];
% Loop through each line of the data
for i = 1:length(data)
% If the line starts with a number, assume it's the hours
if isstrprop(data{i}(1), 'digit') && isempty(hours)
hours = str2double(strsplit(data{i}));
% If the line starts with 'M1', extract the points of interest for M1
elseif startsWith(data{i}, 'M1')
m1_points = extractBetween(data{i}, 'M1 - ', '\n');
m1_points = strsplit(m1_points, ', ');
m1_points(cellfun('isempty', m1_points)) = [];
m1 = [m1; m1_points];
% If the line starts with 'M2', extract the points of interest for M2
elseif startsWith(data{i}, 'M2')
m2_points = extractBetween(data{i}, 'M2 - ', '\n');
m2_points = strsplit(m2_points, ', ');
m2_points(cellfun('isempty', m2_points)) = [];
m2 = [m2; m2_points];
% If the line starts with 'M3', extract the points of interest for M3
elseif startsWith(data{i}, 'M3')
m3_points = extractBetween(data{i}, 'M3 - ', '\n');
m3_points = strsplit(m3_points, ', ');
m3_points(cellfun('isempty', m3_points)) = [];
m3 = [m3; m3_points];
end
end
The data in the .txt file looks like this, and I'm looking to extract the time, alongside the position (307, 327 etc.) for each sample (M1,M2,M3):
General: xxx
CC: 2000
M1 - 277,2209g, OW I.D.xxx, no distinctive features, (160, 220, 295 ?)
M2 - 312,4254g, OW I.D.xxx, no distinctive features,
M3 - 277,7087g, OW I.D.xxx, no distinctive features, (230 ?)
15 10 hours (270)
M1 - 276,9280g, 307, 327, 2mm, 376, 378, 388, 400
M2 - 314,5424g, 366, 371, 393, 398, 3x2, 401
M3 - 278,5357g, 362, 380, 399, 411
16 50 hours (320)
M1 - 277,1215g, 307, 327, 2mm, 360, 376, 378, 389, 400
M2 - 314,6405g, 351, 357, 366, 370, 1mm, 375, 379, 384, 387, 394, 6x5, 401, 403, 409
M3 - 280,6355g, 318, 344, 362, 374, 376, 383, 398, 409, 1x1
Any support on why this code isnt working would be appreciated.
Many thanks
1 Comment
Cris LaPierre
on 12 May 2023
Edited: Cris LaPierre
on 12 May 2023
If it depends on the format of your data, then we will need that. Please attach your file to your post using the paperclip icon.
Also, please share the complete error message (all the red text).
Accepted Answer
More Answers (1)
Walter Roberson
on 12 May 2023
data = textscan(fid, '%s', 'delimiter', '\n');
The %s format always uses cell array of character vectors, never string(), so here data will be a cell with a single entry that contains a cell array of character vectors
data = data{1}; % extract cell array from textscan output
You strip off the outer layer so data will be a cell array of character vectors.
m1_points = extractBetween(data{i}, 'M1 - ', '\n');
data{i} would be one particular character vector. Now what is m1_points after this call?
newStr is a string array if str is a string array. Otherwise, newStr is a cell array of character vectors.
We showed that data{i} is not a string array, so newStr (the output from extractBetween) is going to be a cell array of character vectors even if only one match is found . Note that extractBetween() will find and extract multiple matches if they exist, even if the input is a single character vector, so the fact that you have a single character vector input is not enough to be sure that extractBetween will only find a single match.
Your code is obviously expecting that m1_points will be a character vector, rather than the cell array of character vectors that it actually is.
1 Comment
Peter Kinsley
on 15 May 2023
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!