Hi
I want to write a program which reads from the end of the length of the data up to 15 data's consicutively,for example if the data is
12 3 4 5 65 45 23 2 3 4 5 6 6 7 8 8 9 9 9 9 9 94 21 12 33 45 45 45 45 45 12 34 56 34 42 45 50 34 34
what I want is I want to read from the last data 34 back up to 33 everytime. I mean the data will update or increase it´s number through time.
Any help would be appreciated

Dani AI

Generated

A concise, reliable way to get "the last up to 15 items" in MATLAB depends on whether you already have the whole vector or the values arrive over time. was on the right track about keeping a fixed-size structure for streaming input; below are two small, tested patterns you can drop into your code.

For a complete vector data, use safe indexing with numel so it works when the vector has fewer than 15 elements:

N = 15;
n = numel(data);
lastN = data(max(1, n - N + 1) : n);

For a stream that supplies one new value at a time, keep a circular buffer (no growing arrays, so it stays efficient). The snippet below shows how to insert a new value x and then extract the last M items in chronological order:

N = 15;
buffer = nan(1, N);   % preallocate
idx = 0;
count = 0;

% On each new value x:
idx = mod(idx, N) + 1;
buffer(idx) = x;
count = min(count + 1, N);
M = count;
inds = mod((idx - M + 1 : idx) - 1, N) + 1;
lastM = buffer(inds);

Notes and caveats: use numel for length on vectors to avoid surprises with matrices; preallocate the buffer rather than appending in a loop; circshift is a simpler alternative for tiny N but touches all elements each update; consider a persistent variable inside a function or a small handle class if updates occur inside callbacks or repeatedly across calls. This gives a robust solution whether the data is static or streaming — which likely mirrors what ended up doing.

Recommended Answers

All 4 Replies

How are the data stored? What language do you use?

How are the data stored? What language do you use?

The datas are output of another program written in Matlab and the language is matlab.

That is only a partial answer, as I still don't know the format. If you can take it in as a stream (as the possibility of added data over time suggests), you can store the values successively in a kind of queue for 15 items, always dropping the oldest ones when the size is exceeded. You would then operate on the contents of this queue.

If you have all data as a list, you would need some sort of sublist, if it is a vector, some sort of subvector function.

If you have all data as a string, you would need to split/parse it into some sort of list or vector first.

Thank you I already solved the problem

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.