Hey Thats the code I put and the error:
time1 = AIS1(:,end-4:end)
Index in position 2 is invalid. Array indices must be positive integers or logical values.
Could anybody tell me why?

 Réponse acceptée

Image Analyst
Image Analyst le 16 Sep 2021

0 votes

AIS1 has 4 or fewer columns.
s = size(AIS1)

4 commentaires

flashpode
flashpode le 16 Sep 2021
well that was not the problem I did it another way
thank you
Image Analyst
Image Analyst le 16 Sep 2021
For index 2 to be invalid, it means that end-4 is either 0 or negative. It could be 0 if there were exactly 4 columns in AIS1. It would be negative if AIS1 had only 1, 2, or 3 columns. I'm very sure, from the error you gave, that having 4 or fewer columns was the problem. If it weren't, you would not have gotten that error, you would have gotten no error at all, or would have gotten a different error.
Anyway, I'm glad it's working and thanks for Accepting the Answer.
flashpode
flashpode le 16 Sep 2021
Ah, okay. What I posted was to get the last four digits of each row of the string
If AIS1 is a simple string or character array, you'd do AIS1(end-4:end). If it's an array of strings, say 100 of them, and you wanted only the last 4 characters off each of them in a new 100 element long string where each string is only 4 characters one way is to do this:
% Make an array of strings.
AIS1 = ["1234567", "123456789"]
for k = 1 : numel(AIS1)
% Get this string as a character array.
thisString = char(AIS1(k));
% Get the last 4 characters of this string
% and put into the k'th cell of a cell array.
last4{k} = thisString(end-4:end);
end
% last4 is a cell array where each cell is a character array.
% Convert to a string array if desired:
last4 = string(last4)

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Characters and Strings dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by