Index Out of Bound for 1x1 Array

I am trying to use importdata() method to put the datas within the attached .txt file into an array; data1.
data1 = importdata('4665_10.85.txt');
However, when reshaping it for mean calculation, it shows:
error: data1(6400,_): out of bound 1 (dimensions are 1x1)
I fundamentaly understand the what that means in a sense but as this syntax is very new to me, I am unsure of how I can troubleshoot this.
Subsequent actions after importing the data into the array includes:
ave_number=50;
v1=reshape(data1(1:128*ave_number,3),[128,ave_number]);
v1_ave=mean(v1.');
xx=5.5:0.043:11;
figure(1)
plot(xx,v1_ave);
ylim([0 5])
grid on; hold on;
[minal,idx]=min(v1_ave);
idx
Thank you for any assistence in advance!

Answers (2)

Use readmatrix
data1 = readmatrix('4665_10.85.txt');

3 Comments

Hey there, thanks for the suggestion, unfortunately, the error stack mentioned that readmatrix() is undefined for some reason.
Also, the importdata() method works for certain .txt files' data and not for others, may I check why that might be the case even though both .txt formats should be the same. It works for 4665_5.5.txt but not for 4665_5.55.txt.
clear all
close all
clc
ave_number=50;
wavenumberMeta = '4665_';
wavenumberExtension = '.txt';
excelFilename = 'ConsolidatedData.xlsx';
for waveIncrement = 5.50:0.05:7.00
wavenumber = num2str(waveIncrement);
filename = [wavenumberMeta wavenumber wavenumberExtension];
filename
if isfile(filename)
data1 = [];
data1 = importdata(filename);
#data1 = importdata('clear all
close all
clc
ave_number=50;
wavenumberMeta = '4665_';
wavenumberExtension = '.txt';
excelFilename = 'ConsolidatedData.xlsx';
for waveIncrement = 5.50:0.05:7.00
wavenumber = num2str(waveIncrement);
filename = [wavenumberMeta wavenumber wavenumberExtension];
if isfile(filename)
data1 = [];
data1 = importdata(filename); # Loops through all filename with the name convention, '4665_wavenumber.txt'
v1=reshape(data1(1:128*ave_number,3),[128,ave_number]);
v1_ave=mean(v1.');
xx=5.5:0.043:11;
###########
#figure(1)
#plot(xx,v1_ave);
#ylim([0 5])
#grid on; hold on;
#[minal,idx]=min(v1_ave);
#idx
[minal,idx]=max(v1_ave);
idx
#totalExcelArray
end
end');
v1=reshape(data1(1:128*ave_number,3),[128,ave_number]); ####this is base line
v1_ave=mean(v1.');
xx=5.5:0.043:11;
###########
#figure(1)
#plot(xx,v1_ave);
#ylim([0 5])
#grid on; hold on;
#[minal,idx]=min(v1_ave);
[minal,idx]=max(v1_ave);
idx
end
end
Well the format might need to be consistent. If they are spectroscopic files, can you get the data in standard .spc format so you can use GSTools?
filename = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/858645/4665_5.5.txt';
opt = detectImportOptions(filename);
T = readtable(filename, opt);
data1 = table2array(T);
data1(1:5,:)
ans = 5×3
1.0000 1.0000 3.4240 1.0000 2.0000 3.8510 1.0000 3.0000 4.1770 1.0000 4.0000 3.4990 1.0000 5.0000 3.1260

Sign in to comment.

Well the format might need to be consistent. You seem to have multiple images in there (not sure how many) and they can have different numbers of rows and columns.
If they are spectroscopic files, can you get the data in standard .spc format so you can use GSTools?
I think you're missing some data.
data = readmatrix('4665_5.5.txt');
% Get number of rows and columns from last row
rows = data(:, 1)
columns = data(:, 2)
signal = data(:, 3);
oneStretches = rows == 1;
[~, numImages] = bwlabel(oneStretches)
% Measure locations of the 1's
props = regionprops(oneStretches, 'PixelIdxList');
% See that the signal is rows*columns long.
numValues = numel(signal)
for k = 1 : length(props)
theseLocations = props(k).PixelIdxList;
firstRow = theseLocations(1)
if k == length(props)
lastRow = numValues
else
nextLocations = props(end).PixelIdxList;
lastRow = nextLocations(1) - 1
end
thisSignal = signal(firstRow:lastRow);
rows = data(lastRow, 2);
columns = data(lastRow, 1);
fprintf('This image should be %d by %d.\n', rows, columns)
spectralImage = reshape(thisSignal, rows, columns);
figure
imshow(spectralImage, []);
axis('on', 'image')
impixelinfo;
end

1 Comment

Hey there thanks so much for the recommendation but it turns out that there was just some duplicated string text issue so after cleaning up the txt file properly it works fine! Thank you very much regardless!

Sign in to comment.

Asked:

on 10 Jan 2022

Commented:

on 13 Jan 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!