Error Message "Array indices must be positive integers or logical values" from importing .txt file and get plot

10 views (last 30 days)
I am trying to import a few .txt files and have them plot in the same figure, but I keep getting the following error and have no idea how to fix that.
Error Message:
Array indices must be positive integers or logical values.
Error in Ecoli_K12_20231229_PhosphateBuffer_6 (line 43)
plot(time, Ion,'fileNames', fileNames{fileidx-1});
clc;
clear all;
close all;
% Get list of files.
filelist = dir('G:\Processed PTR Data\20231229_E. coli K12_Phosphate Buffer\Class II Ion\*.txt');
%Loop to read all files:
spectra = cell(size(filelist)); %stored in a file array at first
opts = detectImportOptions(fullfile(filelist(1).folder, filelist(1).name), 'ExpectedNumVariables', 2);
Index exceeds the number of array elements. Index must not exceed 0.
for fileidx = 1:numel(filelist)
spectrum= readtable(fullfile(filelist(fileidx).folder, filelist(fileidx).name), opts); %read file
spectrum.Source = repmat({filelist(fileidx).name}, height(spectrum), 1); %add a variable indicating the source. Maybe you want to use only part of the filename
time = spectrum(:,1)./3600;
spectra{fileidx} = spectrum;
AllSpec(:, fileidx) = spectrum(:, 2);
AllSpecTable = array2table(AllSpec);
Ions = AllSpecTable(:, fileidx);
Ion = table2array(Ions);
fileNames{fileidx} = extractBetween(regexprep(filelist(fileidx).name, '20(\w+)=', 'mQ='), '','.txt');
plot(time, Ion,'fileNames', fileNames{fileidx-1});
hold on;
end
hold off

Answers (1)

Stephen23
Stephen23 on 24 Jan 2024
plot(time, Ion,'fileNames', fileNames{fileidx-1});
% ^^ get rid of this
  2 Comments
Yingxi
Yingxi on 25 Jan 2024
I removed the "-1" and got similar error messages.
Error using plot
Invalid subscript for Y. A table variable subscript must be a
numeric array containing real positive integers, a logical array,
a character vector, a string array, a cell array of character
vectors, or a pattern scalar used to match variable names.
Error in Ecoli_K12_20231229_PhosphateBuffer_6 (line 43)
plot(time, Ion,'fileNames', fileNames{fileidx});
Stephen23
Stephen23 on 25 Jan 2024
"I removed the "-1" and got similar error messages."
No, you get a totally different error message from a totally different bug in your code.
Your code mixes up tables and numeric arrays, with several superfluous conversions between those data types. And that code around ALLSPEC and ALLSPECTABLE is convoluted. Does it work? Given that confusion, such an error is not unexpected.
The basic problem of the error message is that you are trying to plot tables, whereas you should be plotting numeric data:
X = [1;2;3;4];
Y = rand(4,1);
T = table(X,Y);
plot(T{:,1},T{:,2}) % what you should be doing
plot(T(:,1),T(:,2)) % what you are doing
Error using plot
Invalid subscript for Y. A table variable subscript must be a numeric array containing real positive integers, a logical array, a character vector, a string array, a cell array of character vectors, or a pattern scalar used to match variable names.
Look at your data in the variable viewer: the only table you should have is SPECTRUM, the rest should be numeric. Get rid of those TABLE2ARRAY and ARRAY2TABLE calls... simplify your code!
I would start again. Use curly braces to obtain the numeric arrays, not parentheses. Making a lot of assumptions about the sizes and data types, perhaps something like this:
S = dir('G:\Processed PTR Data\20231229_E. coli K12_Phosphate Buffer\Class II Ion\*.txt');
F = fullfile(S(1).folder,S(1).name);
I = detectImportOptions(F, 'ExpectedNumVariables', 2);
for k = 1:numel(S)
F = fullfile(S(k).folder,S(k).name);
T = readtable(F, I);
T{:,'Source'} = {S(k).name};
S(k).data = T;
S(k).ions = T{:,2};
time = T{:,1}./3600; % curly braces!
Ion = T{:,2}; % curly braces!
N = regexprep(S(k).name, {'20(\w+)=','\.txt$'}, {'mQ=',''});
plot(time, Ion, 'DisplayName',N); % correct option name
hold on;
end
hold off
alltables = vertcat(S.data) % depends on the table column names, which you have not told us
allions = horzcat(S.ions)
Of course this is untested code because you did not provide us with any test files.

Sign in to comment.

Categories

Find more on Data Type Conversion 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!