Index Out of Bound for 1x1 Array

조회 수: 3 (최근 30일)
Justin Teh
Justin Teh 2022년 1월 10일
댓글: Justin Teh 2022년 1월 13일
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!

답변 (2개)

David Hill
David Hill 2022년 1월 10일
Use readmatrix
data1 = readmatrix('4665_10.85.txt');
  댓글 수: 3
Image Analyst
Image Analyst 2022년 1월 12일
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?
Walter Roberson
Walter Roberson 2022년 1월 12일
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

댓글을 달려면 로그인하십시오.


Image Analyst
Image Analyst 2022년 1월 12일
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
Justin Teh
Justin Teh 2022년 1월 13일
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!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Data Import and Analysis에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by